2010-10-29 81 views
3

我正在開發ASP.NET 3.5和IIS 7中的應用程序。我編寫了一個HTTP模塊來執行URL重寫,例如,我想重寫用戶名到帳戶ID「〜/ Profiles/profile.aspx?AccountID =」+ account.AccountID.ToString();使用HTTP模塊在ASP.NET 3.5和IIS 7中進行URL重寫

見下:

使用系統; using System.Collections.Generic; using System.Data; using System.Configuration;使用System.IO的 ;使用System.Linq的 ; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;

public class UrlRewrite : IHttpModule 
{ 
    private AccountRepository _accountRepository; 
    private WebContext _webContext; 

    public UrlRewrite() 
    { 
     _accountRepository = new AccountRepository(); 
     _webContext = new WebContext(); 
    } 

    public void Init(HttpApplication application) 
    { 
     // Register event handler. 
     application.PostResolveRequestCache += 
      (new EventHandler(this.Application_OnAfterProcess)); 
    } 

    public void Dispose() 
    { 
    } 

    private void Application_OnAfterProcess(object source, EventArgs e) 
    { 
     HttpApplication application = (HttpApplication)source; 
     HttpContext context = application.Context; 

     string[] extensionsToExclude = { ".axd", ".jpg", ".gif", ".png", ".xml", ".config", ".css", ".js", ".htm", ".html" }; 
     foreach (string s in extensionsToExclude) 
     { 
      if (application.Request.PhysicalPath.ToLower().Contains(s)) 
       return; 
     } 

     if (!System.IO.File.Exists(application.Request.PhysicalPath)) 
     { 
      if (application.Request.PhysicalPath.ToLower().Contains("blogs")) 
      { 

      } 
      else if (application.Request.PhysicalPath.ToLower().Contains("forums")) 
      { 

      } 
      else 
      { 

       string username = application.Request.Path.Replace("/", ""); 

       Account account = _accountRepository.GetAccountByUsername(username); 

       if (account != null) 
       { 
        string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString(); 
        context.Response.Redirect(UserURL); 
       } 
       else 
       { 
        context.Response.Redirect("~/PageNotFound.aspx"); 
       } 
      } 
     } 
    } 
} 

我明白,我需要引用在web.config中這個處理程序來得到它的工作,但我不知道我需要在web.config文件中,並在那裏進入。有些人可以幫我在這裏。另外,是否有其他配置需要這個工作?我需要配置IIS嗎?

在此先感謝。

問候

沃爾特

+0

你試過下面的答案嗎? – 2011-01-09 05:19:03

回答

0

嘗試託管融合的URL重寫,它會爲你節省手工編程的湯姆重寫。

http://urlrewriter.codeplex.com

至少,它表明你如何建立一個處理程序的配置。

關於MF的好處是,它支持自定義模塊正在做你正在做的以上。

6

取決於您是使用IIS7 Classic還是集成管道模式。集成管道模式到這樣做的標準方法如下:

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" /> 
    </modules> 
</system.webServer> 

但是爲了安全起見,你或許想支持IIS7經典/集成與IIS5/6(如果你的開發框適度降級組合使用不同的操作系統),Rick Strahal建議使用的web配置下面的代碼來支持和繞過IIS拋出討厭的錯誤,如果你把它向後兼容:

<system.web> 
    <httpModules> 
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" /> 
    </httpModules> 
</system.web> 
<system.webServer> 
    <validation validateIntegratedModeConfiguration="false"/> 
    <modules runAllManagedModulesForAllRequests="true"> 
    <add name="UrlRewrite" type="MyLibrary.UrlRewrite" /> 
    </modules> 
</system.webServer> 

您還會注意到的另外runAllManagedModulesForAllRequest的=「true」,這是相關的,否則你的HttpModule中的代碼只會在執行時執行瀏覽器會調用由.NET框架管理的.aspx,.ashx,.asmx等文件。

此外,爲了實際重寫url(而不是重定向用戶),您將需要在事件處理程序中使用context.RewritePath(string)方法。

這種工作方式是,application.Request.Path用「友好」的字符串,我想看起來像這樣在你的應用進來:

http://www.domain.com/robertp

其中被改寫爲:

http://www.domain.com/Profiles/profile.aspx?AccountID=59

要做到這一點,而不是使用context.Response.Redirect(),您將需要使用context.RewritePath()如下:

string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString(); 
context.RewritePath(UserURL); 

... TADA !!

這應該可以確保傳遞給服務器的網址是profiles.aspx?AccountID=59,而用戶在瀏覽器上獲得更友好的robertp

至於配置選項,只要你堅持IIS7你應該罰款與上面的Web配置設置。它當您嘗試在你的開發PC上運行的IIS6或IIS5的測試,你可能有一個問題,這通常會迴避的事實圍繞該robertp不具有可識別的文件擴展名,以便您的HttpModule代碼不會被執行,除非你添加一個文件擴展名使用.NET ISAPI。

希望是有用的。

+0

親愛的史蒂文。非常感謝你的回答。我只注意到你的迴應。我一直沒有收到有關此問題的答案通知 - 看起來我沒有將該功能切換爲「開啓」。我會嘗試你的答案並在這裏發佈回覆。如果我記得,我遇到的其中一個問題是使用Response.Redirect,我注意到你的答案使用RewritePath,所以我認爲這個答案肯定能解決我的問題。感謝您花時間回覆。 – 2011-01-17 22:20:54

+0

沒問題。希望它有效。 – 2011-01-18 12:52:25