IgnoreRoute with webservice - Exclude asmx URLs from routing

不问归期 提交于 2019-11-30 06:55:47

I got it to work using this (a combo of other answers):

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

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(/.*)?" } );

What happens when you use:

routes.IgnoreRoute("FileVistaControl/filevista.asmx");

If that doesn't work, try using the ASP.NET Routing Debugger to help you: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Try this:

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

This is based on a Phil Haack recommendation stated here.

Have you tried:

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

It would help if you posted the source for your route configuration. I'm going to take a shot in the dark and say to make sure that your IgnoreRoute() calls are all at the top of your routing definition.

The way IgnoreRoute works is to create a route that matches the ignored route URL and constraints, and attaches a StopRoutingHandler as the RouteHandler. The UrlRoutingModule knows that a StopRoutingHandler means it shouldn't route the request.

As we know, the routes are matched in the order of which they are defined. So, if your {controller}/{action}/{id} route appears before your "FileVistaControl/filevista.asmx/GetLanguageFile/" route, then it will match the "{controller}/{action}/{id}" route.

I may be totally off base here, but it's hard to know without seeing your source. Hope this helps. And post source code! You'll get better answers.

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