Reputation: 684
i am quite new in asp.net mvc. so i have few questions about routing.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
);
}
}
above code mention default controller and action but when we run site from vs2013 ide how asp.net engine understand it has to show default controller and action?
when we mention controller and action name in url then how asp.net engine understand to load mention controller and action from url because we do not define route for all controller and action. just we define default controller and action in route.
discuss in details.
Upvotes: 0
Views: 186
Reputation: 56688
Routing engine tries to match the actual URL to one of the route forms that were configured. In your cases there is only one default route, so it will take a look at the URL trying to match it to {controller}/{action}/{id}
pattern. "default" here means "use this one if none is provided". So on runtime the routing engine parses the URL and checks what parts of the route are actually given, and replaces those that are not with defaults.
A few examples:
example.com
- nothing is given, use default for action and controllerexample.com/MyController
- controller is given, action is not, use default for action. Note that with this route it is impossible to specify action without a controller.example.com/MyController/MyAction
- both controller and action are given, do not use any defaultsexample.com/MyController/MyAction/123
- same as above but also id is specifiedexample.com/MyController/MyAction/foo/bar
- bar
would not match anything and cause an errorUpvotes: 0