Friday, May 3, 2013

ASP.NET MVC routing


MVC routing

In ASP.NET MVC one of the main parts is routing. The routing is helps to map the particular view and particular controller.
When we create an ASP.NET MVC application routing is preconfigured in web.config and Global.asax

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

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

protected void Application_Start()
{
      RegisterRoutes(RouteTable.Routes);
}

The above code shows preconfigured routing on Global.asax.
When we run the our an ASP.NET MVC application the request comes to "Application_Start()"

 The method inside we called the routing configuration method so at the same time RegisterRoutes method also fired and registered the routing configurations.
RegisterRoutes method has some default routing tables, the default routing table has some segments, and the segments have arguments.
First we look at the first segment "name". It is the name of the routing table.
Second segment "URL" have some arguments, the first argument mention the controller name second argument mention the action method and the third argument mention the query string .
The third segment "defaults" provide the some default values. Occasionally we don't have provided any controller, action and id that time it would be took the given default values.

More detail about routing  read the scottgu blog 

No comments:

Post a Comment