【问题标题】:Is HttpContext.RemapHandler supposed to change which handler processes request?HttpContext.RemapHandler 是否应该更改哪个处理程序处理请求?
【发布时间】:2012-03-19 14:05:33
【问题描述】:

MSDN 文档说:

HttpContext.RemapHandler 方法 - 使您能够为请求指定处理程序。

我正在尝试根据第一个处理程序中的条件将请求的处理从一个处理程序移动到另一个处理程序。 HttpContext.RemapHandler 方法似乎初始化了第二个处理程序的实例,但没有调用 HttpHandler.ProcessRequest 方法;响应为空。

HttpContext.RemapHandler 方法是否做了我认为应该做的事情——将处理转移到一个新的 HttpHandler 并调用 HttpHandler.ProcessRequest 方法?还是我应该使用另一种方法,例如另一种方法或 HttpModule?

编辑: 原来我应该使用 HTTPHandlerFactory。我的解决方案现在运行良好。

那么 HttpContext.RemapHandler 到底是干什么用的?

【问题讨论】:

    标签: c# httphandler remap


    【解决方案1】:

    您可以按照您的指定使用HttpContext.RemapHandler,但是如果另一个HttpHandler 调用RemapHandler(例如,在PostResolveRequestCache 中注册MvcHandler 的ASP.NET MVC)您的IHttpModule 将永远不会触发。这也许就是为什么从未调用过IHttpHandler.Process

    如果这是您的问题,您可以像这样在MvcApplication.RegisterRoutes 中简单地定义一个要忽略的路由:

    routes.IgnoreRoute("your_path/{*pathInfo}");
    

    另外,请记住,对于 Visual Studio Web 开发服务器和 IIS6,RemapHandler 将不起作用。

    这是一个示例,说明如何根据 Integrated Pipeline 是否已激活并且仍然能够访问会话来选择正确的方式来重新映射处理程序:

    public void Init(HttpApplication application)
    {
      if (HttpRuntime.UsingIntegratedPipeline)
        // For IIS 7 and IIS 8
        application.PostAuthorizeRequest += Application_PostAuthorizeRequest;
      else
        // For IIS 6
        application.PostMapRequestHandler += Application_PostMapRequestHandler;
    }
    
    private void Application_PostAuthorizeRequest(object sender, EventArgs e)
    {
        ((HttpApplication)sender).Context.RemapHandler(_myHandler);
    }
    
    private void Application_PostMapRequestHandler(object sender, EventArgs e)
    {
        ((HttpApplication)sender).Context.Handler = _myHandler;
    }
    

    在这种情况下使用HttpHandlerFactoryHttpModule 之间的区别在于,后者允许您决定何时使用哪个IHttpHandler,而不管ASP.NET 的IHttpHandler 映射。有关 MSDN 的更多信息:HTTP Handlers and HTTP Modules Overview

    【讨论】:

    • 明确的答案,尤其是关于 mvc 处理程序的部分!。谢谢!
    • 如果您应用了 .NET 2.0 Service Pack 2,RemapHandler 将在 PostAuthorizeRequest 中正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2011-09-26
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多