ASP.NET MVC routing and static data (ie. images, scripts, etc)

*爱你&永不变心* 提交于 2019-11-30 20:48:35
Svetlozar Angelov

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

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

Here is what I read from here:

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.

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.)

By default the routing engine will ignore route maps for all files that exist physically on the server. In short, you need to do nothing for a MVC app to link to static files.

You can also do a little trick in IIS. I store my js, images, css etc in the Content folder underneath the virtual directory.

If you then view properties (in IIS manager) of the Content folder, create it as a virtual directory, then remove the Wildcard mapping. Then set the Content folder back to a normal directory. This should then stop requests to these files being handled by the aspnet_isapi handler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!