2011-04-20 50 views
1

響應我有相似內容的兩個生產網站。需要通過搜索引擎索引的一個這些網站和其他不應該。是否有添加內容提供給使用HTTP模塊的客戶端響應的方式?使用的HttpModules修改發送到客戶端

以我爲例,我需要的HTTP模塊添加到發送到當模塊是特定的網絡上活躍的響應。

回答

2

你可能要處理的事件PreRequestHandlerExecute應用,因爲它是IHttpHandler過程之前只需運行網頁本身:

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

    public void Init(HttpApplication context) 
    { 
    context.PreRequestHandlerExecute += AttachNoIndexMeta; 
    } 

    private void AttachNoIndexMeta(object sender, EventArgs e) 
    { 
    var page = HttpContext.Current.CurrentHandler as Page; 
    if (page != null && page.Header != null) 
    { 
     page.Header.Controls.Add(new LiteralControl("<meta name=\"robots\" value=\"noindex, follow\" />")); 
    } 
    } 
} 

做的另一種方式,就是創建自己的Stream實施和應用它通過Response.Filters,但肯定棘手。

相關問題