This is a beginner level question for asp.net MVC
I have the following code in global.asax.cs
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 = (string)null } // Parameter defaults
);
}
in Homecontroller.cs i have updated the Index method as follows
public ActionResult Index(string id)
{
ViewData["Message"] = "Welcome to ASP.NET MVC1!"+ id;
return View();
}
My understanding is, if I give the url http://localhost/mvc1/default/1 it should work
instead it is throwing up 404 error
any help what is the reason behind this
I’m assuming your application is called “mvc1” and that’s the root of your project. If that’s the case:
So “default” is the name if your route, not the name of the action. Basically what the routing engine does is look for a controller and action that matches requests coming in. Given the route you have setup, it would break down like this:
If certain parts of the route are omitted, it will attempt to fill in the missing values with the defaults you have specified. As you can see, there is no controller named
DefaultControllerin your project, and thus it uses the default you’ve specified which isHome. It then tries to find an action method calleddefaultand fails again, so it uses the default value in your route, which isIndex. Finally, you have 2 segments left in your URL, and no route matches that pattern (2 segments after the action), so it can’t find the right place to go.What you need to do is remove one of your segments, and this should work. Routing can be a little tricky, so I would recommend reading up on it.