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

C# 如何创建自定义httpHandler并将其添加到现有IIS网站,c#,iis,httphandler,C#,Iis,Httphandler,我需要向现有IIS网站添加自定义httpHandler。我有一个带IIS的Windows Server 2012 R2,在IIS中,我有一个运行ASP.NET解决方案的网站,我无法访问源代码。ApplicationPool配置为与.Net 4.0一起以集成模式运行 我们想开发一个定制的httpHandler作为.dll,并在网站的Handler Mappings下注册它。为此,我们在Visual Studio 2015中创建了一个新的动态链接库项目,其代码如下: using System; us

我需要向现有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的完整路径和一个友好的名称。在处理程序映射->我的处理程序->右键单击->请求限制->映射->未选中“仅当请求映射到时调用处理程序:”

该处理程序现在列在启用的处理程序下。现在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等)都会进入处理程序执行。从

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

您真正需要的是一个HTTPModule,它将钩住每个请求并执行响应

HTTP模块是一个程序集,该程序集在被调用的每个请求上都被调用 根据你的申请做的

下面是一个示例实现

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>

,

请注意,您在代码中使用了将请求传输到goole.com的功能,这是不可能的。server.transfer只能用于在同一请求上下文中传输请求,而不能传输到其他网站或域