c# - 如何忽略 MVC 路由中的所有特定文件扩展名

标签 c# asp.net-mvc routing

如何配置 IgnoreRoute 以忽略所有具有特定扩展名的文件,而不管它们位于哪个目录?

如果我这样做,一切正常,我的 Flash 电影开始播放:

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

因此“123”和“456”部分是可变的,可以是任何整数。显然,我不想为每部电影都做其中一个,也不需要用可变占位符替换 123 和 456。这只是一种目录类型的示例,整个应用程序中都存储了 Flash 电影,所以我需要的是一个 IgnoreRoute 值,它将忽略具有 .flv 扩展名的文件,无论它们在层次结构中的什么位置.

我试过以下方法:

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

到目前为止,我唯一能做的就是专门将完整的相对路径传递给 FLV 文件。有什么建议吗?

最佳答案

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

关于c# - 如何忽略 MVC 路由中的所有特定文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4684012/

相关文章:

c# - 创建一个 'website builder' - 我将如何构建它?

routing - 重定向到 Angular 2 路由中的新页面

android - 带 map 和路线的 Android 项目要使用哪些库?

reactjs - 路径路由 : Application Load Balancer for React Application

asp.net-mvc - Multi-Tenancy w/NHibernate+CaSTLe Windsor(单个应用程序,多个数据库)

c# - asp.net网站如何集成Paypal支付网关

c# - 在C#.NET4应用程序中在后台处理多个输入

c# - 如何实现一个非随机的随机数生成器?

c# - 表单验证: Determine why ticket is invalid

c# - Linq-To-SQL 多次访问数据库