如何路由到CSS / JS文件mvc.net [英] 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文件,也就是说,我想有区1 /内容/的site.css
将 /content/site.css
或 /content/area1/site.css
。
感谢
推荐答案
我没有找到MVC路由这样做的方式是我落得这样做是:
我一个HTTP模块在运行此code:
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)(/.*)?" });
要prevent mvc.net从路由这些请求。
to prevent mvc.net from routing these requests.
人们也可能需要通过mvc.net设置IIS6 / IIS7将请求路由到静态文件,但我忘了细节。
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.
这篇关于如何路由到CSS / JS文件mvc.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!