3 回答

TA貢獻(xiàn)1797條經(jīng)驗 獲得超4個贊
在研究了這個問題幾個小時之后,我發(fā)現(xiàn)僅添加忽略規(guī)則將為您的靜態(tài)文件提供服務(wù)。
在RegisterRoutes(RouteCollection路由)中,添加以下忽略規(guī)則:
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{file}.html");

TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊
為什么不使用IIS來做到這一點?您可以創(chuàng)建重定向規(guī)則,以將從第一個路由到第二個路由的所有請求指向該請求,甚至可以將其到達(dá)您的應(yīng)用程序。因此,這將是重定向請求的更快方法。
假設(shè)您擁有IIS7 +,則可以執(zhí)行以下操作:
<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:
而且,如果您想知道重寫規(guī)則的去向,這里是其在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>

TA貢獻(xiàn)1865條經(jīng)驗 獲得超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 關(guān)注
- 623 瀏覽
添加回答
舉報