我正在開發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嗎?
在此先感謝。
問候
沃爾特
你試過下面的答案嗎? – 2011-01-09 05:19:03