2014-11-21 39 views
0

我有一個在Classic ASP.NET中開發並在IIS 7.5上運行的網站。它工作正常。但現在我有任務將附屬程序添加到我的網站。我希望我的反向鏈接看起來像www.mywebsite.com/r/userid。好吧,我用Google搜索了一圈,發現我可以:重寫只有一個子路徑的最快方法

  1. 使用UrlRewrite第三方HttpModule秒。據我所知,他們是基於runAllManagedModulesForAllRequests="true" web.config設置。理論上,我可以:

  2. 手動設置runAllManagedModulesForAllRequests="true"RewritePathApplication_BeginRequest。但我的Application_BeginRequest已經包含了一些代碼。由於一個很少被稱爲URL的地址,因此將所有頁面,圖像等發送到Application_BeginRequest太重了。

所以,問題是我怎麼能只改寫與www.mywebsite.com/r/啓動子路徑,並且不叫Application_BeginRequest的每張圖片,CSS等?最好如果沒有第三方的東西。

回答

0

那麼,完成了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); 
}