IgnoreRoute with webservice - 从路由中排除asmx URL

boz*_*boz 11 asp.net asp.net-mvc routing web-services asmx

我将filevistacontrol添加到我的asp.net MVC Web应用程序中.

我有一个在路由中被忽略的media.aspx页面

routes.IgnoreRoute("media.aspx");
Run Code Online (Sandbox Code Playgroud)

这可以成功运行并提供标准的webforms页面.

添加filevistacontrol后,我似乎无法忽略控件对其web服务的任何调用.

例如,以下ignoreRoute似乎仍然被MvcHandler接收.

routes.IgnoreRoute("FileVistaControl/filevista.asmx/GetLanguageFile/");
Run Code Online (Sandbox Code Playgroud)

抛出的异常是:

'The RouteData must contain an item named 'controller' with a non-empty string value'
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Jot*_*aBe 12

简短回答:

routes.IgnoreRoute( "{*url}", new { url = @".*\.asmx(/.*)?" } );
Run Code Online (Sandbox Code Playgroud)

答案很长:

如果您的服务可以位于路径的任何级别,则这些选项都不适用于所有可能的.asmx服务:

routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)

默认情况下,路由模式中的参数将匹配,直到找到斜杠.

如果参数以星号开头*,就像pathInfo那些答案一样,它将匹配所有内容,包括斜杠.

所以:

  • 第一个答案只适用.asmx于根路径中的服务,因为它{resource}不会匹配斜杠.(可以用于类似的东西http://example.com/weather.asmx/forecast)
  • 第二个只适用于.asmx距离根目录一级的服务.{directory}将匹配路径的第一个段和{resource}服务的名称.(可以用于类似的东西http://example.com/services/weather.asmx/forecast)

没有人会工作http://example.com/services/weather/weather.asmx/forecast)

解决方案是使用该IgnoreRoute方法的另一个重载,它允许指定约束.使用此解决方案,您可以使用匹配所有URL的简单模式,如下所示:{*url}.然后,您只需设置一个约束,检查此URL是否指向.asmx服务.这个约束可以用这样的正则表达式表示:.*\.asmx(/.*)?.此正则表达式匹配任何以字符串结尾的字符串,.asmx后面跟一个斜杠和后面的任意数量的字符.

所以,最终的答案是这样的:

routes.IgnoreRoute( "{*url}", new { url = @".*\.asmx(/.*)?" } );
Run Code Online (Sandbox Code Playgroud)


jef*_*cco 8

我用它来工作(其他答案的组合):

routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)