IgnoreRoute with webservice - Exclude asmx URLs from routing
Im ad开发者_如何学编程ding the filevistacontrol to my asp.net MVC web application.
I have a media.aspx page that is ignored in the routing with
routes.IgnoreRoute("media.aspx");
This works successfully and serves a standard webforms page.
Upon adding the filevistacontrol, I can't seem to ignore any calls the control makes to it's webservice.
Eg the following ignoreRoute still seems to get picked up by the MvcHandler.
routes.IgnoreRoute("FileVistaControl/filevista.asmx/GetLanguageFile/");
The exception thrown is:
'The RouteData must contain an item named 'controller' with a non-empty string value'
Thanks in advance.
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 likehttp://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 likehttp://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(/.*)?" } );
I got it to work using this (a combo of other answers):
routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
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.
精彩评论