Nov 9, 2015

Understanding Routing in ASP.NET MVC

Routing Engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller. We can find the following piece of code in Application_Start() method ofGlobal.asax file.

We can find RouteConfig.cs file under App_Start folder in ASP.NET MVC 5 application. If we follow this method inRouteConfig class, we will find one default configured route as follows. Line # 03 to #07 is configuring one default route.



protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
  FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
   RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

}

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 routes.MapRoute(
      name: "Default",
     url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
     );

}


No comments:

Post a Comment