Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IgnoreRoute with webservice - Exclude asmx URLs from routing

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("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.

like image 686
boz Avatar asked Jan 12 '11 23:01

boz


2 Answers

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(/.*)?" } );
like image 101
JotaBe Avatar answered Oct 16 '22 06:10

JotaBe


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

routes.IgnoreRoute("{directory}/{resource}.asmx/{*pathInfo}");
like image 44
jeffsaracco Avatar answered Oct 16 '22 08:10

jeffsaracco