【问题标题】:URL Rewrite in ASP.NET 3.5 and IIS 7 using HTTP Module使用 HTTP 模块在 ASP.NET 3.5 和 IIS 7 中重写 URL
【发布时间】: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


【解决方案1】:

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

<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 中的代码将仅在浏览器调用由 .aspx、.ashx、.asmx 等管理的文件时执行.NET 框架。

此外,为了真正重写 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);

...多田!!

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

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

希望有用。

【讨论】:

  • 亲爱的史蒂文。非常感谢您的回答。我才注意到你的回复。我没有收到关于这个问题的答案通知 - 看起来我没有“打开”该功能。我将尝试您的回答并在此处发布回复。如果我记得,我遇到的问题之一是 Response.Redirect 我注意到您的答案使用了 RewritePath 所以我认为这个答案肯定会解决我的问题。感谢您抽出宝贵时间回复。
【解决方案2】:

试试 Managed Fusion URL Rewriter,它将为您节省手动编程的汤姆。

http://urlrewriter.codeplex.com

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

MF 的好处在于它支持自定义模块,可以完全按照您在上面所做的工作。

【讨论】:

    猜你喜欢
    • 2012-06-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2017-08-07
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多