views:

3551

answers:

3

I'd like to ignore multiple wildcard routes. With asp.net mvc preview 4, they ship with:

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

I'd also like to add something like:

RouteTable.Routes.IgnoreRoute("Content/{*pathInfo}");

but that seems to break some of the helpers that generate urls in my program. Thoughts?

+4  A: 

This can be quite tricky.

When attempting to figure out how to map route data into a route, the system currently searches top-down until it finds something where all the required information is provided, and then stuffs everything else into query parameters.

Since the required information for the route "Content/{*pathInfo}" is entirely satisfied always (no required data at all in this route), and it's near the top of the route list, then all your attempts to map to unnamed routes will match this pattern, and all your URLs will be based on this ("Content?action=foo&controller=bar")

Unfortunately, there's no way around this with action routes. If you use named routes (f.e., choosing Html.RouteLink instead of Html.ActionLink), then you can specify the name of the route to match. It's less convenient, but more precise.

IMO, complex routes make the action-routing system basically fall over. In applications where I have something other than the default routes, I almost always end up reverting to named-route based URL generation to ensure I'm always getting the right route.

Brad Wilson
+9  A: 

There are two possible solutions here.

  1. Add a constraint to the ignore route to make sure that only requests that should be ignored would match that route. Kinda kludgy, but it should work.

    RouteTable.Routes.IgnoreRoute("{folder}/{*pathInfo}", new {folder="content"});

  2. What is in your content directory? By default, Routing does not route files that exist on disk (actually checks the VirtualPathProvider). So if you are putting static content in the Content directory, you might not need the ignore route.

Haacked
A: 

@Haacked This is where it gets a little tricky, and I suppose I should have included this info with my post. I have a handler css.axd that trims my css files. it's setup to execute from any directory. So a common situation is to have requests to /Content/css/css.axd?url=Content/css/global.css or something similar. I know I could force it to always run out of the root directory, but I didn't want to introduce yet another unknown.

@Brad Wilson Thanks for the info. I ran into the exact situation you wrote about.

At this point, I think my best solution is to modify the url to just do something like /css.axd?url=Content/css/global.css. This way, the axd file will be ignored by the default rule that mvc provides ({resource}.axd/{*pathInfo}).

Jim Geurts