How to ignore all defined file extensions in MVC routing

How do I configure IgnoreRoute to ignore all files with a specific extension, no matter what directory they are in?

If I do this, everything will work, and my Flash movie will play:

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

So, sections "123" and "456" are variables and can be any integer. It is obvious, however, that I do not want to do one of them for each NOR movie, I need to replace 123 and 456 with variable placeholders. This is just an example of one type of directory, Flash movies are stored in the entire application, so I need an IgnoreRoute value that will ignore files with the .flv extension no matter where they are in the hierarchy .

I tried the following:

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

The only thing I can do so far is to pass the full relative path to the FLV file. Any suggestions?

+4
source share
1 answer

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

In short, we did not want routing to try to route requests for static files such as images. Unfortunately, this caused us a headache when we remembered that many ASP.NET functions make requests for .axd files that do not exist on disk.

To fix this, we have included a new extension method in the RouteCollection, IgnoreRoute, which creates a route mapped to the StopRoutingHandler route handler (a class that implements IRouteHandler). Effectively, any request that matches the "ignore route" will be ignored by routing and normal ASP.NET processing will be based on the existing HTTP rendering handler.

Therefore, in our default template, you will notice that we have the following route defined.

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

This handles standard .axd requests.

........

We allow only one route, and this should 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 am I doing here? Eilon’s technique showed me which should display all the URLs of these routes, but then limit which routes to ignore using the Dictionary restrictions. Thus, in this case, these routes will match (and therefore ignore) all requests for favicon.ico (no matter which directory), as well as requests for the .aspx file. Since we talked about routing to ignore these requests, the usual processing of these ASP.NET requests is.

+5
source

Source: https://habr.com/ru/post/1335449/


All Articles