3 回答
![?](http://img1.sycdn.imooc.com/5c4aa098000126bb09600960-100-100.jpg)
TA贡献1797条经验 获得超4个赞
在研究了这个问题几个小时之后,我发现仅添加忽略规则将为您的静态文件提供服务。
在RegisterRoutes(RouteCollection路由)中,添加以下忽略规则:
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{file}.html");
![?](http://img1.sycdn.imooc.com/545868b60001587202200220-100-100.jpg)
TA贡献1780条经验 获得超5个赞
为什么不使用IIS来做到这一点?您可以创建重定向规则,以将从第一个路由到第二个路由的所有请求指向该请求,甚至可以将其到达您的应用程序。因此,这将是重定向请求的更快方法。
假设您拥有IIS7 +,则可以执行以下操作:
<rule name="Redirect Static Images" stopProcessing="true">
<match url="^static/?(.*)$" />
<action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" />
</rule>
或者,如果您不需要重定向,如@ ni5ni6所示:
<rule name="Rewrite Static Images" stopProcessing="true">
<match url="^static/?(.*)$" />
<action type="Rewrite" url="/a/b/c/{R:1}" />
</rule>
为@RyanDawkins编辑2015-06-17:
而且,如果您想知道重写规则的去向,这里是其在web.config文件中位置的映射。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- rules go below -->
<rule name="Redirect Static Images" stopProcessing="true">
<match url="^static/?(.*)$" />
<action type="Redirect" url="/a/b/c/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
![?](http://img1.sycdn.imooc.com/5458662500019a7c02200220-100-100.jpg)
TA贡献1865条经验 获得超7个赞
我有类似的问题。我最终使用了HttpContext.RewritePath:
public class MyApplication : HttpApplication
{
private readonly Regex r = new Regex("^/static/(.*)$", RegexOptions.IgnoreCase);
public override void Init()
{
BeginRequest += OnBeginRequest;
}
protected void OnBeginRequest(object sender, EventArgs e)
{
var match = r.Match(Request.Url.AbsolutePath);
if (match.Success)
{
var fileName = match.Groups[1].Value;
Context.RewritePath(string.Format("/a/b/c/{0}", fileName));
}
}
}
- 3 回答
- 0 关注
- 600 浏览
添加回答
举报