Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding routing in Global.asax (asp.net-mvc)

In Global.asax what does the following signify?

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");     
like image 464
PadmakarKulkarni Avatar asked Jul 28 '09 07:07

PadmakarKulkarni


3 Answers

This is one of the really frustrating things about learning MVC - the documentation for this feature is awful - there's just hardly anything there: http://msdn.microsoft.com/en-us/library/dd470170(VS.100).aspx.

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

This allows all the something.axd files to run outside of MVC - that "{*pathInfo}" at the end allows query strings to be ignored (it's kind of a wildcard).

Note that this doesn't apply any such wildcard to the path, so:

trace.axd?clear=1 //excluded from MVC

mySubFolder/customResource.axd //MVC passed to mySubFolderController.customResource()

Helpful. I've been unable to find any decent documentation on exactly what is and isn't supported as keywords apart from "{resource}" and "{*pathInfo}"

However there is an almost completely undocumented feature that gives you a lot more control over these ignored routes:

//ignore all WebForms .aspx/.asmx/.ashx calls anywhere
routes.IgnoreRoute( "{*allaspx}", new { allaspx = @".*\.as[pmh]x(/.*)?" } );

If you pass an anon-initialised object with a property, that property becomes a keyword that you can use in the route.

You can't pass a regex in the route, but you can in this anon property.

like image 120
Keith Avatar answered Nov 15 '22 21:11

Keith


An .axd file is a virtual file that is handled by an HTTP Handler. They are used for (amongst other things) delivering various resources to the webpage, such as automatically generated javascript for AJAX controls and the like.

As these are virtual files, you do not want the routing engine to try to map these requests to controllers. You need them to be executed directly by ASP.NET.

That is what the line achieves.

like image 43
Greg B Avatar answered Nov 15 '22 22:11

Greg B


Without this ASP.NET would try to map all requests to AXD handlers to controllers and actions. Having the ignoreRoute means the URL will not map the URL to a controller as per the default behaviour.

like image 29
RichardOD Avatar answered Nov 15 '22 21:11

RichardOD