【问题标题】:Fastest way to rewrite only one subpath只重写一个子路径的最快方法
【发布时间】:2014-11-22 01:02:35
【问题描述】:
我有一个使用 Classic ASP.NET 开发并在 IIS 7.5 上运行的网站。它工作正常。但现在我的任务是向我的网站添加会员计划。我希望我的 reflink 看起来像 www.mywebsite.com/r/userid。好吧,我搜索了一下,发现我可以:
使用 UrlRewrite 第三方 HttpModules。据我了解,它们基于runAllManagedModulesForAllRequests="true" web.config 设置。理论上,我可以:
手动设置runAllManagedModulesForAllRequests="true" 并在Application_BeginRequest 中执行RewritePath。但是我的Application_BeginRequest 已经包含了一些代码。将所有页面、图像等发送到Application_BeginRequest 太重了,因为一个很少调用的 URL。
所以,问题是如何只重写以www.mywebsite.com/r/ 开头的子路径,而不是为每个图像、css 等调用Application_BeginRequest?如果没有第三方的东西最好。
【问题讨论】:
标签:
asp.net
url-rewriting
url-routing
【解决方案1】:
好了,HttpModule 结束了。
web.config:
<system.webServer>
<modules>
<add name="ReflinkModule" preCondition="" type="www.ReflinkModule" />
</modules>
</system.webServer>
ReflinkModule.cs:
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}
public void BeginRequest(Object source, EventArgs e)
{
if (HttpContext.Current == null)
return;
if (HttpContext.Current.Request == null)
return;
if (HttpContext.Current.Request.RawUrl == null)
return;
string start = "/r/";
if (!HttpContext.Current.Request.RawUrl.StartsWith(start))
return;
string key = HttpContext.Current.Request.RawUrl.Substring(start.Length);
HttpContext.Current.RewritePath("~/Xxxxx.aspx?r=" + key);
}