ASP.NET MVC路由和静态数据(即图像,脚本等) [英] ASP.NET MVC routing and static data (ie. images, scripts, etc)

查看:165
本文介绍了ASP.NET MVC路由和静态数据(即图像,脚本等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在ASP.NET MVC1(或2)Web应用程序中请求静态资源的请求,请说...图像,JavaScript文件或CSS文件.... NET框架尝试查看请求是否与路由列表匹配...最终找不到它的控制器?

If I have a request for a resource in my ASP.NET MVC1 (or 2) web app for a static resource, say ... an image or a javascript file or a css file ... does the .NET framework try and see if the request matches the route list ... and eventually can't find a controller for it?

例如。

资源:/Content/Images/Foo.png

Resource: /Content/Images/Foo.png

此请求是否通过我的路由列表..无法与此匹配任何控制器/动作

Does this request go through my route list .. fails to match any controllers / actions to this request and then attempt that path directly?

推荐答案

您可以选择是映射现有文件还是不设置 RouteCollection.RouteExistingFiles 属性

You can choose whether to map an existing file or not setting the RouteCollection.RouteExistingFiles Property


获取或设置一个值,该值指示
ASP.NET路由是否应处理
与现有文件匹配的URL。

Gets or sets a value that indicates whether ASP.NET routing should handle URLs that match an existing file.

这是我从此处读取的内容:


但是,路由系统仍会检查文件系统以查看
传入URL是否恰好与文件或磁盘匹配,并且如果是这样,路由将忽略该请求(通过URL可能也匹配的
绕过任何路由条目),以便直接提供文件。
这对于静态文件(例如图像,CSS和JavaScript文件)非常方便。您可以
将它们保留在您的项目中(例如,在/ Content或/ Script文件夹中),然后引用和
直接为它们提供服务,就好像您根本没有使用路由一样。由于该文件确实在磁盘上存在
,因此它优先于您的路由配置。

However, the routing system still does check the file system to see if an incoming URL happens to match a file or disk, and if so, routing ignores the request (bypassing any route entries that the URL might also match) so that the file will be served directly. This is very convenient for static files, such as images, CSS, and JavaScript files. You can keep them in your project (e.g., in your /Content or /Script folders), and then reference and serve them directly, just as if you were not using routing at all. Since the file genuinely exists on disk, that takes priority over your routing configuration.

如果相反,您希望您的路由配置优先于上的文件磁盘上,可以将RouteCollection的RouteExistingFiles属性的
设置为true。 (默认情况下为假。)

If, instead, you want your routing configuration to take priority over files on disk, you can set the RouteCollection’s RouteExistingFiles property to true. (It’s false by default.)

这篇关于ASP.NET MVC路由和静态数据(即图像,脚本等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆