I am completely new to ASP.Net MVC. I just created an MVC3 project in Visual Studio 2010. The view engine is razor. When I just ran the application it gave the proper result in the browser. The URL is http://localhost:4163/ . Then I applied “Set as Start Page” to Index.cshtml inside ~\Views\Home folder. Then when I ran the application the url became http://localhost:4148/Views/Home/Index.cshtml and it said the resource cannot be found. What do I do to correct it? Where is the url mapping done?
Global.asax file:
using System.Web.Mvc;
using System.Web.Routing;
namespace TEST
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
To see your View like this:
In the xController.cs file, under Controllers folder,
make sure you add an ActionResult method like this:
I also got this message when I wasn't referencing my model name correctly:
@using (Html.BeginForm("MyView", "MyModel", FormMethod.Get))
Unbelievably, I'd accidentally deleted the public keyword from the controller!
I imagine this will help precisely no one else, but you never know ...
Just adding this one in here - if anyone is having issues where the index page on the Home Controller no longer loads, it might be because you haven't setup your StartUp Project yet. Right-click the project that you want your app to start at and set it as the startup project, and you should be good to go.
in the address bar on browser input like this: ControllerName/ActionMethod not input ControllerName/ViewName for example in address bar: http://localhost:55029/test2/getview cshtml file code:
and cs file code:
(MVC 5): Change
RouteConfig.cs
to include tworoutes
like this:so when the language part of the route is not specified it does not mistake the controller names which not match regex
"[a-z]{2}"
and replaces default language and redirects to rest of route...I too got the same html-404 error:
But after close examination, I found that I had left the name of the controller as
Default1Controller
instead of changing it toHomeController
. When I made the change and debugged the app it worked. Hope it will help you if you have the same problem.