Sunday 24 July 2016

MVC Routing Engine

Posted by : Manav Pandya

Routing engine in MVC Application


The ASP.NET MVC 4 Internet Application template in Visual Studio includes two default routes so that the project will run. 

Above photo shows the default routes. 

The first route instructs the routing engine to ignore requests for .axd resources such as "ScriptResource.axd"  and  "WebResource.axd " .

 These resources don’t physically exist as files in the application; rather, they are HttpModules that load special content (such as images, scripts, CSS, etc.) that are embedded in DLL files so that they can be sent to the browser as part of the response. 

The second route is used to map requests to controllers and action methods.

For example :


when we run MVC 4.0  project its shows in URL 

http:\\servername\controller\view

It means MVC 4.0 application maintains RouteConfig.cs file

Syntax is as follow :


{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

where : 


  1. controller  - shows default controller name (Home is in example)
  2. action        - View name contained within controller(Index is in example)
  3. id               -  Optional parameter