问题描述
所以我在 IIS 7 下运行的 MVC 3 应用程序中有这样的路由:
So I have a route like this in my MVC 3 application running under IIS 7:
routes.MapRoute(
"VirtualTourConfig",
"virtualtour/config.xml",
new { controller = "VirtualTour", action = "Config" }
);
诀窍在于/virtualtour/config.xml 中确实存在一个文件.似乎请求只是返回该位置的 xml 文件,而不是访问处理 XML、进行一些更改并返回自定义 XmlResult 的路由.
The trick is that a file actually exists at /virtualtour/config.xml. It seems like the request is just returning the xml file at that location instead of hitting the route, which processes the XML, makes some changes and returns a custom XmlResult.
如果文件存在于磁盘上,关于如何告诉我的应用程序点击路由而不是实际文件的任何建议?
Any suggestions on how I can tell my application to hit the route and not the actual file in the event that the file exists on disk?
看来我可以在 Global.asax 的 RegisterRoutes 方法中使用 routes.RouteExistingFiles = true;
来告诉应用程序忽略磁盘上的文件.然而,这会全局设置标志并破坏应用程序中的许多其他请求.例如,我仍然希望调用/assets/css/site.css 来返回 CSS 文件,而不必为每个静态资产专门设置路由.所以现在问题变成了,有没有办法在每条路线的基础上做到这一点?
It appears that I can use routes.RouteExistingFiles = true;
in the RegisterRoutes method of Global.asax to tell the application to ignore files on disk. This, however, sets the flag globally and breaks a lot of other requests within the application. For example, I still want calls to /assets/css/site.css to return the CSS file without having to specifically set routes up for each static asset. So now the question becomes, is there a way to do this on a per-route basis?
推荐答案
到目前为止,我发现的最佳答案是全局应用 routes.RouteExistingFiles=true
然后有选择地忽略路由我想传递到现有文件,如 .js、.css 等.所以我最终得到了这样的东西:
So far the best answer to this that I have found is to globally apply routes.RouteExistingFiles=true
and then selectively ignore the routes I want to pass through to existing files like .js, .css, etc. So I ended up with something like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("*.js|css|swf");
routes.RouteExistingFiles = true;
routes.MapRoute(
"VirtualTourConfig",
"virtualtour/config.xml",
new { controller = "VirtualTour", action = "Config" }
);
}
如果有人有更好的解决方案,我想看看.我更喜欢有选择地将RouteExistingFIles"标志应用于个别路线,但我不知道是否有办法做到这一点.
If anyone has a better solution, I'd like to see it. I'd much prefer to selectively apply an "RouteExistingFIles" flag to individual routes but I don't know if there's a way to do that.
这篇关于当文件实际存在于指定位置时的 MVC 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!