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

标签 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;
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 等())都会到达您的处理程序以执行。来自 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,它将 Hook 每个请求并做出响应。

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 中还有许多其他处理程序,它们可能会在您的处理程序获取请求之前干扰请求。检查 thisthis

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

关于c# - 如何创建自定义 httpHandler 并将其添加到现有 IIS 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44978576/

相关文章:

c# - 获取抛出异常的方法名

c# - ASP.Net 缓存破坏方法不起作用

asp.net-mvc - 如何从 HTTPHandler 动态调用 Controller 操作?

IIS 随机重启 Dotnet 核心应用

c# - 使用 HttpHandler 继续处理页面以保护子文件夹

javascript - Array.insert 函数错误作为 Object function Array() { [native code] } has no method 'insert'

c# - ServiceStack 服务与业务逻辑分离

c# - 如何在新线程上打开一个窗口?

c# - WCF 和日期时间解析?

angular - 在重定向到新的 SPA 页面时在 header 中传递身份验证 token