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

后端 未结 4 2160
南旧
南旧 2021-02-08 11:18

In Global.asax what does the following signify?

 routes.IgnoreRoute(\"{resource}.axd/{*pathInfo}\");     
4条回答
  •  甜味超标
    2021-02-08 12:09

    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.

提交回复
热议问题