Server error in '/' application in ASP.NET MVC 3 with Razor engine

I just created a new ASP.NET MVC 3 project with the Razor engine. I added the controller to the controller folder, and then in homController.cs I added the view.

The view ( index.cshtml ) has only this code:

 @{ ViewBag.Title = "Home"; } <h2>Home</h2> 

And when I start debugging, it shows me this error:

 Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: / Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225 

What is the problem?

+6
source share
6 answers

Could you check App_Start / RouteConfig.cs, you should have this code for the controller named homController.cs:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "hom", action = "Index", id = UrlParameter.Optional } ); } 

I suppose your first controller name is not "Home", so you need to change the default controller name!

+6
source

Check your routing entries in Application_Start in global.asax since you are using MVC 3. For example, "Joffrey Kern" suggested you set up routes.

also make sure your controller is named "HomeController" and you have a public method called "Index" that returns an ActionResult object.

+2
source

In ASP.NET MVC 3, your routes are defined in global.asax

You can find it at the root of the site.

By default, it looks like this:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } 

This means that when you invoke the root of the site without parameters, it uses the default values ​​- in this case home/index .

So you need to make sure that you:

  • there is a controller called home
  • a Action is called index , which returns an ActionResult

Alternatively, you can update the default values ​​in routes, although if you are just starting out, I would not recommend this.

+2
source

These are not necessarily your routes ... These may be faulty configurations in the web configuration.

0
source

If you get this error, I highly recommend that you change your public class name. Give a new name as "HomeController":

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MyFirstMVC.Controllers { public class HomeController : Controller { // GET: /First/ public string Index() { return "Jay Swaminarayan"; // this string will be display at your run time!!! } } } 
0
source

This is a shell of akula

Please find this answer.

  • Right click on mvc project

  • Select Properties

  • Select the web tab

  • Select "Specific Page"

  • Assuming you have a controller called HomeController and the action is an Index method, enter "home / index" instead of home / index. cshtml in the text box corresponding to the button "specific page".

-1
source

Source: https://habr.com/ru/post/947976/


All Articles