2017-07-07 89 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在相同的請求上下文中,而不是其他網站或域

相關問題