2015-11-30 43 views
0

我想在我的IHttpModule中的我的.aspx.ashx文件的響應中添加一些標題。我如何過濾.axdIsSystemHandler()中的所有其他系統處理程序?我可以爲.aspxHttpContext.Current.Handler is Page,但.ashx呢?確定HttpContext.Current.Handler是我的或系統的

public class MySecurityModule : IHttpModule 
{ 
    public void Init(HttpApplication application) 
    { 
     application.PreSendRequestHeaders += PreSendRequestHeaders; 
    } 

    void PreSendRequestHeaders(object sender, EventArgs e) 
    { 
     if (HttpContext.Current.Handler == null) 
      return; 
     if (IsSystemHandler()) 
      return; 
     HttpContext.Current.Response.Headers.Add(.....); 
    } 

    bool IsSystemHandler() 
    { 
     // How to filter system handlers here? 
    } 
} 

回答

0
bool IsSystemHandler() 
{ 
    Type t = context.Handler.GetType(); 
    while (t != null && t.Assembly != this.GetType().Assembly) 
     t = t.BaseType; 
    if (t == null) 
     return true; 
    return false; 
} 
+0

雖然此代碼段可以解決的問題,[包括一個解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-基於代碼-答覆)真的有助於提高你的文章的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – NathanOliver

相關問題