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

陌路散爱 提交于 2019-12-09 05:24:53

问题


In Global.asax what does the following signify?

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

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


It tells the routing engine to ignore this request and leave it to ASP.NET Webforms to handle things.

This is useful for example ELMAH logging that uses the handler elmah.axd.



来源:https://stackoverflow.com/questions/1192446/understanding-routing-in-global-asax-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!