【问题标题】:How to create and add custom httpHandler to existing IIS WebSite如何创建自定义 httpHandler 并将其添加到现有 IIS 网站
【发布时间】:2017-12-12 05:05:45
【问题描述】:

我需要将自定义 httpHandler 添加到现有的 IIS 网站。我有一个带有 IIS 的 Windows Server 2012 R2,在 IIS 中我有一个运行 ASP.NET 解决方案的网站,我无法访问源代码。 ApplicationPool 配置为在 .Net 4.0 和集成模式下运行。

我们想开发一个自定义的 httpHandler 作为 .dll 并在网站的 Handler Mappings 下注册它。为此,我们在 Visual Studio 2015 中创建了一个新的动态链接库项目,代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace MinimalStandaloneHttpHandler
{
    public class Class1 : IHttpHandler
    {
        public Class1()
        {

        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest Request = context.Request;
            HttpResponse Response = context.Response;
            // This handler is called whenever a file ending 
            // in .sample is requested. A file with that extension
            // does not need to exist.

            context.Server.Transfer("http://www.google.com", false);
        }

        public bool IsReusable
        {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            get { return false; }
        }

    }
}

我们已经编译了它,然后转到 IIS -> 网站 -> 处理程序映射 -> 添加通配符脚本映射。

这里我们添加了“*”作为请求路径、.dll 的完整路径和一个友好的名称。在 Handler Mappings -> My Handler -> Right click -> Request Restrictions -> Mapping -> Unchecked "Invoke handler only if request is mapped to:"。

处理程序现在列在启用的处理程序下。现在 web.config 被修改了:

<configuration>
    <system.webServer>
        <handlers>
            <add name="asdasd" path="*" verb="*" modules="IsapiModule" scriptProcessor="C:\inetpub\wwwroot\WebSiteStaticTest\MinimalStandaloneHttpHandler.dll" resourceType="File" requireAccess="None" preCondition="bitness32" />
        </handlers>
    </system.webServer>
</configuration>

但是当我们在网站上执行页面时,处理程序似乎不起作用,因为我们没有被重定向到 Google。这里有什么问题?

【问题讨论】:

    标签: c# iis httphandler


    【解决方案1】:

    我看到您在要响应所有请求的路径中使用了 *。 HTTPhandler 通常用作您注册特定类型请求的端点,例如 *.mspx,其中所有类型的 mspx 请求(default.mspx,home.mspx 等()都会到达您的处理程序执行。来自MSDN

    ASP.NET HTTP 处理程序是进程(通常称为 “端点”)响应对 ASP.NET Web 的请求而运行 应用。最常见的处理程序是 ASP.NET 页面处理程序,它 处理 .aspx 文件。

    您真正需要的是一个 HTTPModule,它将挂钩到每个请求并做出响应。

    HTTP 模块是一个程序集,它会在每个请求上调用 为您的应用程序制作。

    所以看看this,这里有一个示例实现。

    using System;
    using System.Web;
    public class HelloWorldModule : IHttpModule
    {
        public HelloWorldModule()
        {
        }
    
        public String ModuleName
        {
            get { return "HelloWorldModule"; }
        }
    
        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            application.BeginRequest += 
                (new EventHandler(this.Application_BeginRequest));
    
        }
    
        private void Application_BeginRequest(Object source, 
             EventArgs e)
        {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Redirect("http://www.google.com", false);
        }
    
    
        public void Dispose() { }
    }
    

    并像这样注册模块

    <configuration>
    <system.webServer><modules><add name="HelloWorldModule" type="HelloWorldModule"/></modules></system.webServer>
    </configuration>
    

    您也可以添加通配符处理程序(就像您所做的那样),但在 asp.net 中有许多其他处理程序可能会在您的处理程序获取请求之前干扰请求。检查 this ,this

    请注意,您在代码中使用Server.Transfer 将请求传输到goole.com,这是不可能的。服务器。传输只能用于在同一请求上下文中传输请求,不能传输到另一个网站或域

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2011-01-26
      • 2017-08-31
      • 2011-01-06
      相关资源
      最近更新 更多