如何创建自定义 httpHandler 并将其添加到现有 IIS 网站

How to create and add custom httpHandler to existing IIS WebSite

我需要向现有 IIS 网站添加自定义 httpHandler。我有一个带 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 的完整路径和友好的名称。在处理程序映射下 -> 我的处理程序 -> 右键单击​​ -> 请求限制 -> 映射 -> 未选中 "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。这里有什么问题?

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

An ASP.NET HTTP handler is the process (frequently referred to as the "endpoint") that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files.

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

An HTTP module is an assembly that is called on every request that is made to your application.

因此请查看 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 中还有许多其他处理程序可能会在您的处理程序获取 it.Check this ,this[= 之前​​干扰请求18=]

请注意,您在代码中使用Server.Transfer将请求转移到goole.com,而不是possible.server.Transfer只能用于转移请求请求上下文,而不是另一个网站或域