IgnoreRoute with webservice - Exclude asmx URLs from routing

前端 未结 6 693
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 08:48

Im adding the filevistacontrol to my asp.net MVC web application.

I have a media.aspx page that is ignored in the routing with

routes.IgnoreRoute(\"m         


        
6条回答
  •  悲哀的现实
    2020-12-30 09:46

    Short answer:

    routes.IgnoreRoute( "{*url}", new { url = @".*\.asmx(/.*)?" } );
    

    Long answer:

    If your service can be in any level of a path, none of these options will work for all possible .asmx services:

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

    By default, the parameters in a route pattern will match until they find a slash.

    If the parameter starts with a star *, like pathInfo in those answers, it will match everything, including slashes.

    So:

    • the first answer will only work for .asmx services in the root path, becasuse {resource} will not match slashes. (Would work for something like http://example.com/weather.asmx/forecast)
    • the second one will only work for .asmx services which are one level away from the root.{directory} will match the first segment of the path, and {resource} the name of the service. (Would work for something like http://example.com/services/weather.asmx/forecast)

    None would work for http://example.com/services/weather/weather.asmx/forecast)

    The solution is using another overload of the IgnoreRoute method which allows to specify constraints. Using this solution you can use a simple pattern which matches all the url, like this: {*url}. Then you only have to set a constraint which checks that this url refers to a .asmx service. This constraint can be expressed with a regex like this: .*\.asmx(/.*)?. This regex matches any string which ends with .asmx optionally followed by an slash and any number of characters after it.

    So, the final answer is this:

    routes.IgnoreRoute( "{*url}", new { url = @".*\.asmx(/.*)?" } );
    

提交回复
热议问题