2015-09-03 25 views
1

我是新來的IIS模塊和C#區域。IIS模塊和C#:如何在發送給客戶端之前修改頁面內容

我需要在網站將頁面內容發送給客戶端之前,修改網站特定目錄中靜態HTML文件的內容。修改包括添加橫幅,頁腳等。

基於我的研究,我應該能夠通過IIS模塊實現我的目標(正確?)。這裏是我的代碼:

namespace MyProject 
{ 
    public class MyModule : IHttpModule 
    { 
     #region IHttpModule Members 

     public void Dispose() 
     { 

     } 

     public void Init(HttpApplication context) 
     { 
      context.PreSendRequestContent += 
       new EventHandler(onPreSendRequestContent); 
     } 

     #endregion 

     public void onPreSendRequestContent(Object source, EventArgs e) 
     { 
      HttpApplication app = (HttpApplication)source; 
      HttpRequest request = app.Context.Request; 
      HttpResponse response = app.Context.Response; 

      if (request.Path.Contains("my_specific_directory")) 
      { 
       //add banner and footer to the page content 
       //which class, methods, or properties to use? 
      } 
     } 
    } 
} 

我不確定PreSendRequestContent是否是開始修改頁面內容的正確事件。有人能指點我正確的方式來獲取由IIS檢索的頁面內容嗎?

感謝和問候。

回答

3

我認爲最好使用MVC框架,因爲它很容易維護,你可以做任何事情。但是,如果您仍想通過IIS HTTP模塊修改靜態HTML,那麼就是這個過程。希望這可以幫助。

首先,添加處理程序&構建提供程序以通過IIS處理靜態HTML文件。

Web.config文件:

<system.webServer> 
    <handlers> 
     <add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" /> 
    </handlers> 
</system.webServer> 

<system.web> 
    <compilation> 
     <buildProviders> 
      <add extension=".html" type="System.Web.Compilation.PageBuildProvider" /> 
     </buildProviders> 
    </compilation> 
</system.web> 

接下來,寫HTTP模塊&過濾器來修改你的HTML內容。

HTTP模塊:

class ModifyContentModule : IHttpModule 
{ 
    public void Dispose() { } 

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += (o, e) => 
     { 
      context.Response.Filter = new ModifyContentStream(context.Response.Filter); 
     }; 
    } 
} 

篩選:

public class ModifyContentStream : Stream 
{ 
    private Stream _base; 
    private MemoryStream _memoryBase = new MemoryStream(); 

    public ModifyContentStream(Stream stream) 
    { 
     _base = stream; 
    } 

    public override void Write(byte[] buffer, int offset, int count) 
    { 
     _memoryBase.Write(buffer, offset, count); 
    } 

    public override void Flush() 
    { 
     // Get static HTML code 
     string html = Encoding.UTF8.GetString(_memoryBase.GetBuffer()); 

     // Modify your HTML 
     // Sample: Replace absolute links to relative 
     Regex regex = new Regex("(href=\")http:\\/\\/www\\.example\\.com(\\/[^\"']+\\.[^\"']+\")"); 
     Match match = regex.Match(html); 
     while (match.Success) 
     { 
      string oldValue = match.Value; 
      string newValue = match.Groups[1].Value + match.Groups[2].Value; 
      html = html.Replace(oldValue, newValue); 

      match = match.NextMatch(); 
     } 

     // Flush modified HTML 
     byte[] buffer = Encoding.UTF8.GetBytes(html); 
     _base.Write(buffer, 0, buffer.Length); 
     _base.Flush(); 
    } 

    #region Rest of the overrides 
} 

}

最後,HTTP模塊添加到你的web.config

Web.config

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <remove name="ModifyContent" /> 
     <add name="ModifyContent" type="ModifyContentModule.ModifyContentModule, ModifyContentModule" /> 
    </modules> 
    <handlers> 
     <add name="html" verb="GET,HEAD,POST,DEBUG" path="*.html" type="System.Web.UI.PageHandlerFactory" /> 
    </handlers> 
</system.webServer> 
+0

感謝您的致電! – curious1

+0

'ModifyContentStream'中的其他重寫怎麼樣?我們是否把它們作爲'throw new NotImplementedException();'? – Blaise

+0

如果您的網站不是MVC網站,您如何構建和部署HTTP模塊? – GGirard

相關問題