2017-07-07 59 views
1

我需要将自定义httpHandler添加到现有IIS网站。我有一个Windows Server 2012 R2与IIS和IIS中我有一个WebSite运行一个ASP.NET解决方案,我无法访问源。 ApplicationPool被配置为以.Net 4.0和集成模式运行。如何创建自定义httpHandler并将其添加到现有IIS网站

我们想开发一个自定义的httpHandler作为.dll,并在Handler Mappings下的WebSite中注册它。要做到这一点,我们在Visual Studio 2015年一个新的动态链接Libary项目,下面的代码创建:

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> 

但是,当我们执行该网站上的页面处理程序似乎并没有工作,因为我们没有得到重定向到谷歌。这里有什么问题?

回答

1

我看到您在要响应所有请求的路径中使用了*。 HttpHandler的通常被用作在您注册特定类型的请求,说* .mspx终点,所有类型MSPX请求(default.mspx,home.mspx等()涉及到你的处理程序execution.From MSDN

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

你真正需要的是一个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许多其他处理程序可以处理程序之前,干扰请求得到的it.Check thisthis

请注意,你在你的代码中使用Server.Transfer传输请求goole.com,这是不possible.server.Transfer只能用来传输reque st在相同的请求上下文中,而不是其他网站或域

相关问题