Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore route in asp.net forms url routing

Tags:

I am using the .NET 3.5 SP1 framework and I've implemented URL routing in my application. I was getting javascript errors:

Error: ASP.NET Ajax client-side framework failed to load.
Resource interpreted as script but transferred with MIME type text/html.
ReferenceError: Can't find variable: Sys

Which I believe is because my routing is picking up the microsoft axd files and not properly sending down the javascript. I did some research and found that I could use Routes.IgnoreRoute, which should allow me to ignore the axd like below:

Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

But, when I add that line to my Global.asax I get this error:

CS1061: 'System.Web.Routing.RouteCollection' does not contain a definition for 'IgnoreRoute' and no extension method 'IgnoreRoute' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)

I've got the System.Web.Routing namespace imported, any ideas?

like image 896
Austin Avatar asked Nov 07 '08 20:11

Austin


People also ask

What does the Ignoreroute () method do?

Ignores the specified URL route for the given list of available routes.

How do you override the prefix route?

We can override the RoutePrefix on a method, using the tilde(~) sign. We can easily define parameterized templates in the attribute based routing.

Which of the following method prevents routing from handling some of the request in ASP.NET MVC?

Ignore() method to prevent routing from handling certain requests. What is the use of action filters in an MVC application?


2 Answers

You don't need to reference ASP.NET MVC. You can use the StopRoutingHandler which implements IRouteHandler like so:

routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler())); 

This is part of .NET 3.5 SP1 and doesn't require MVC. The IgnoreRoutes method is a convenience extension method which is part of ASP.NET MVC.

like image 145
Haacked Avatar answered Oct 08 '22 16:10

Haacked


An old question but in case it still helps anyone, this worked for me:

routes.Ignore("{resource}.axd/{*pathInfo}"); 

The "Ignore" method exists, whereas in standard ASP.NET the "IgnoreRoute" method appears not to (i.e., not using MVC). This will achieve the same result as Haacked's code, but is slightly cleaner ...

like image 44
Ed Graham Avatar answered Oct 08 '22 18:10

Ed Graham