Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore all of a particular file extension in MVC routing

How do I configure an IgnoreRoute to ignore all files with a certain extension, regardless of what directory they're in?

If I do this, everything works and my Flash movie gets played:

routes.Ignore("community/123/plan/456/video/moviename.flv");

So the "123" and "456" sections are variable and can be any integer number. Obviously though, I don't want to do one of these for each movie NOR do I have a need to replace 123 and 456 with variable placeholders. This is only an example of one type of directory, there are Flash movies stored throughout the application so what I need is an IgnoreRoute value that will ignore files that have a .flv extension no matter where in the hierarchy they are.

I've tried the following:

routes.IgnoreRoute("{file}.flv");
routes.IgnoreRoute("(.*).flv(.*)"); // Yeah I know, I'm horrible at RegEx

The only thing I can get to work so far is specifically passing the full relative path to the FLV file. Any suggestions?

like image 762
Scott Avatar asked Jan 13 '11 18:01

Scott


People also ask

What will be used when we don't want to use routing for a particular extension?

Effectively, any request that matches an “ignore route” will be ignored by routing and normal ASP.NET handling will occur based on existing http handler mappings. Hence in our default template, you'll notice we have the following route defined. routes. IgnoreRoute("{allflv}", new { allflv = @".

What does the IgnoreRoute () method do?

IgnoreRoute(RouteCollection, String, Object)Ignores the specified URL route for the given list of the available routes and a list of constraints.

What is routes IgnoreRoute resource Axd * pathInfo?

routes.IgnoreRoute("{resource}.axd/{*pathInfo}") That is telling the routing engine that we will not be processing those requests that match that route pattern. In other words, we will not process .


1 Answers

Check this article by Phil Haack: http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

Long story short, we didn’t want routing to attempt to route requests for static files such as images. Unfortunately, this caused us a headache when we remembered that many features of ASP.NET make requests for .axd files which do not exist on disk.

To fix this, we included a new extension method on RouteCollection, IgnoreRoute, that creates a Route mapped to the StopRoutingHandler route handler (class that implements IRouteHandler). Effectively, any request that matches an “ignore route” will be ignored by routing and normal ASP.NET handling will occur based on existing http handler mappings.

Hence in our default template, you’ll notice we have the following route defined.

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

This handles the standard .axd requests.

........

We only allow one catch-all route and it must happen at the end of the URL. However, you can take the following approach. In this example, I added the following two routes.

routes.IgnoreRoute("{*allaspx}", new {allaspx=@".*\.aspx(/.*)?"});
routes.IgnoreRoute("{*favicon}", new {favicon=@"(.*/)?favicon.ico(/.*)?"}); 

What I’m doing here is a technique Eilon showed me which is to map all URLs to these routes, but then restrict which routes to ignore via the constraints dictionary. So in this case, these routes will match (and thus ignore) all requests for favicon.ico (no matter which directory) as well as requests for a .aspx file. Since we told routing to ignore these requests, normal ASP.NET processing of these requests will occur.

like image 96
Chandu Avatar answered Sep 30 '22 13:09

Chandu