如何路由到 mvc.net 中的 css/js 文件 [英] how to route to css/js files in mvc.net
问题描述
我正在尝试使用 mvc.net 中的路由向我的应用程序添加一个区域.对于我添加的控制器:
I am trying to add an area to my application using routing in mvc.net. For controllers i added:
routes.MapRoute(
"Area1", // Route name
"Area1/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我如何以相同的方式路由 css/js 文件,即我想让 area1/content/site.css
转到 /content/site.css
code> 或 /content/area1/site.css
.
how can i route the css/js files in the same way, i.e. i would like to have area1/content/site.css
going to /content/site.css
or to /content/area1/site.css
.
谢谢
推荐答案
我没有找到用 mvc 路由来做到这一点的方法,我最终做的是:我在 http 模块中运行了这段代码:
I did not find a way of doing this with mvc routing what i ended up doing is: I ran this code in a http module:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication Application = sender as HttpApplication;
var match = r.Match(Application.Request.Url.AbsolutePath);
if (match.Success)
{
var fileName = match.Groups[2].Value;
Application.Context.RewritePath("/" + fileName);
}
}
r 在我的例子中是一个正则表达式:
r is a regex in my case:
private readonly Regex r = new `Regex("^/gmail(/canvas)?/((content|scripts|images|tinymce).*)$", RegexOptions.IgnoreCase);`
在 global.asax 中我添加了:
in global.asax i added:
routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*.(css|js|gif|jpg)(/.*)?" });
防止 mvc.net 路由这些请求.
to prevent mvc.net from routing these requests.
可能还需要设置 iis6/iis7 以通过 mvc.net 将请求路由到静态文件,但我忘记了详细信息.
one might also have to set iis6/iis7 to route requests to static files through mvc.net but i forgot the details.
我从几个我不记得的帖子中选择了这个方法,所以我很抱歉我不能给予适当的信任.
I picked this method up from several posts that i cannot remember so i apologize that i cannot give proper credit.
这篇关于如何路由到 mvc.net 中的 css/js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!