【发布时间】:2010-10-29 09:45:17
【问题描述】:
我正在 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 吗?
提前致谢。
问候
沃尔特
【问题讨论】:
-
你试过下面的答案了吗?
标签: http iis-7 url-rewriting httpmodule