How to ignore route in asp.net forms url routing
Asked Answered
C

4

23

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?

Chadbourne answered 7/11, 2008 at 20:1 Comment(0)
B
40

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.

Bahr answered 9/11, 2008 at 15:47 Comment(1)
How would I ignore *.php files in ASP.NET Core (MVC 6)? It is a lot different!Weeden
R
8

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

Roadwork answered 25/10, 2011 at 13:2 Comment(1)
True but the Ignore method is available starting from .Net 4. OP is using 3.5 SP1.Expropriate
R
3

I would just like to add that you also need to make sure the order of your IgnoreRoutes rule is in the the correct order otherwise your first route will be applied first and your IgnoreRoute will... well be ignored.

Recha answered 5/4, 2009 at 22:54 Comment(0)
W
1

MapRoute and IgnoreRoute are extension methods in System.Web.Mvc --- do you have that assembly referenced properly?

Wilona answered 7/11, 2008 at 20:9 Comment(1)
I'm not using MVC, so I don't have that assembly referenced. Do I need to download the MVC assembly separately to be able to use IgnoreRoute, or should it be part of .NET 3.5 SP1?Chadbourne

© 2022 - 2024 — McMap. All rights reserved.