使用HTTP模块在ASP.NET 3.5和IIS 7中重写URL

使用HTTP模块在ASP.NET 3.5和IIS 7中重写URL,http,iis-7,url-rewriting,httpmodule,Http,Iis 7,Url Rewriting,Httpmodule,我正在用ASP.NET 3.5和IIS 7开发一个应用程序。我已经编写了一个HTTP模块来执行URL重写,例如,我想将用户名重写为帐户ID“~/Profiles/profile.aspx?AccountID=“+Account.AccountID.ToString() 见下文: 使用制度; 使用System.Collections.Generic; 使用系统数据; 使用系统配置; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.Sec

我正在用ASP.NET 3.5和IIS 7开发一个应用程序。我已经编写了一个HTTP模块来执行URL重写,例如,我想将用户名重写为帐户ID“~/Profiles/profile.aspx?AccountID=“+Account.AccountID.ToString()

见下文:

使用制度; 使用System.Collections.Generic; 使用系统数据; 使用系统配置; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.HTMLControl; 使用System.Web.UI.WebControl; 使用System.Web.UI.WebControl.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中引用这个处理程序才能让它工作,但我不知道我需要在web.config文件中输入什么以及在哪里输入。请帮我拿一些。另外,是否需要任何其他配置才能使其正常工作?我需要配置IIS吗

提前谢谢

问候


Walter

试试托管Fusion URL重写器,它将为您节省大量的手动重写编程

至少它向您展示了如何在配置中设置处理程序


MF的优点在于它支持定制模块,这正是您在上面所做的

取决于您使用的是IIS7经典还是集成管道模式。使用集成管道模式执行此操作的标准方法如下:

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

这应该确保传递到服务器的url是具有
profiles.aspx?AccountID=59
的url,而用户在浏览器上获得更友好的
robertp

至于配置选项,只要您坚持IIS7,就可以使用上面的web配置设置。当您尝试在运行IIS6或IIS5的Dev PC上测试时,您可能会遇到问题,这通常会围绕以下事实展开:
robertp
没有可识别的文件扩展名,因此除非添加使用.NET ISAPI的文件扩展名,否则不会执行HttpModule代码


希望这有用。

您尝试过下面的答案吗?亲爱的史蒂文。非常感谢你的回答。我刚刚注意到你的反应。我还没有收到关于这个问题的答复通知-看起来我没有“打开”该功能。我会尝试你的答案并在这里发布回复。如果我记得的话,我遇到的一个问题是回复。重定向,我注意到你的答案使用了重写路径,所以我认为这个答案肯定会解决我的问题。感谢您花时间回复。
<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>
string UserURL = "~/Profiles/profile.aspx?AccountID=" + account.AccountID.ToString();
context.RewritePath(UserURL);