[英]URL Rewrite in ASP.NET 3.5 and IIS 7 using HTTP Module
我正在ASP.NET 3.5和IIS 7中開發一個應用程序。我編寫了一個HTTP模塊來執行URL重寫,例如,我想將用戶名重寫為帳戶ID“〜/ Profiles / profile.aspx?AccountID =” + account.AccountID.ToString();
見下文:
使用系統; 使用System.Collections.Generic; 使用System.Data; 使用System.Configuration; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.HtmlControls; 使用System.Web.UI.WebControls; 使用System.Web.UI.WebControls.WebParts; 使用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中引用這個Handler來使它工作,但我不知道我需要在web.config文件中輸入什么以及在哪里。 有人可以幫幫我嗎。 此外,是否需要任何其他配置才能使其工作? 我需要配置IIS嗎?
提前致謝。
問候
沃爾特
取決於您使用的是IIS7 Classic還是集成管道模式。 使用Integrated Pipeline模式執行此操作的標准方法如下:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewrite" type="MyLibrary.UrlRewrite" />
</modules>
</system.webServer>
但為了安全起見,您可能希望在IIS5 / 6上支持IIS7 Classic / Integrated和優雅降級的組合(如果您的開發盒使用不同的操作系統), 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/Profiles/profile.aspx?AccountID=59
要執行此操作而不是使用context.Response.Redirect()
您將需要使用context.RewritePath()
,如下所示:
string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString();
context.RewritePath(UserURL);
... TADA !!
這應確保傳遞到服務器的URL是具有profiles.aspx?AccountID=59
的URL,而用戶在其瀏覽器上獲得更友好的robertp
。
至於配置選項,只要你堅持使用IIS7就可以使用上面的web配置設置了。 當您嘗試在運行IIS6或IIS5的Dev PC上進行測試時,您可能會遇到問題,這通常會圍繞robertp
沒有可識別的文件擴展名這一事實,因此除非您添加文件擴展名,否則您的HttpModule代碼將不會被執行使用.NET ISAPI。
希望這很有用。
嘗試使用托管融合URL重寫器,它將為您節省手動編程的Tom以進行重寫。
http://urlrewriter.codeplex.com
它至少會告訴你如何在配置中設置一個處理程序。
關於MF的好處是它支持自定義模塊完全按照上面的方式執行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.