MVC路由当一个文件确实存在在指定位置 [英] MVC routing when a file actually exists at the specified location
问题描述
所以我有这样的路线在我MVC 3应用程序IIS 7下运行:
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?
编辑:看来,我可以使用 routes.RouteExistingFiles = TRUE;
在Global.asax中的的RegisterRoutes方法来告诉应用程序忽略磁盘上的文件。然而,在全球设置了标志,并打破了很多应用程序中的其他请求。例如,我还是要呼吁/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" }
);
}
如果任何人有一个更好的解决方案,我想看到它。我更preFER有选择地应用了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路由当一个文件确实存在在指定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!