确定HttpContext.Current.Handler是我的或系统的

时间:2015-11-30 23:57:24

标签: asp.net ihttphandler ihttpmodule

我想在.aspx中的.ashxIHttpModule文件的回复中添加一些标题。如何在.axd中过滤IsSystemHandler()和所有其他系统处理程序?我HttpContext.Current.Handler is Page可以.aspx.ashx怎么办?

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?
    }
}

1 个答案:

答案 0 :(得分: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;
}