How to create and add custom httpHandler to existing IIS WebSite

放肆的年华 提交于 2021-01-21 10:51:34

问题


I need to add a custom httpHandler to an existing IIS WebSite. I have a Windows Server 2012 R2 with IIS and in IIS i have a WebSite which runs an ASP.NET solution where i have no access to the sources. The ApplicationPool is configured to run with .Net 4.0 and in Integrated Mode.

We want to develop a custom httpHandler as .dll and register this within the WebSite under Handler Mappings. To do so we have created in Visual Studio 2015 a new Dynamic Linked Libary project with the following code:

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; }
        }

    }
}

We have compiled it and the went to IIS -> WebSite -> Handler Mappings -> Add Wildcard Script Map.

Here we added "*" as Request Path, the full path to the .dll and a friendly name. Under Handler Mappings -> My Handler -> Right click -> Request Restrictions -> Mapping -> Unchecked "Invoke handler only if request is mapped to:".

The handler is now listed under the enabled handlers. Now the web.config got modified:

<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>

But when we execute the page on the website the handler does not seem to work, cause we are not getting redirected to Google. What is wrong here?


回答1:


I see you have used * in the path which you want to respond to all requests. HTTPhandler is normally used as end point where you register to particular type of requests say *.mspx where all type of mspx requests(default.mspx ,home.mspx etc() comes to your handler for 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.

What you really need is an HTTPModule which will hook into every request and do a response.

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

So look into this ,here's a sample implementation.

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() { }
}

And register the module like this

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

You can also add a wildcard handler (like you did) but there are many other handlers in asp.net which can interfere the request before your handler get's it.Check this ,this

Please note that you used Server.Transfer in your code to transfer request to goole.com,which is not possible.server.Transfer can only used to transfer request on the same request context,not to another website or domain



来源:https://stackoverflow.com/questions/44978576/how-to-create-and-add-custom-httphandler-to-existing-iis-website

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!