简体   繁体   English

为什么我不能在httpModule中获得Page.Title?

[英]Why can't I get the Page.Title within an httpModule?

I am tracking page titles in an ASP.net webforms application. 我正在ASP.net Webforms应用程序中跟踪页面标题。 The code below works however page.Title always throws a System.NullReferenceException even though the page I am tracking has a title. 但是下面的代码可以正常工作page.Title始终抛出System.NullReferenceException,即使我正在跟踪的页面具有标题。 Why? 为什么?

public class TrackingModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.PostAcquireRequestState += new EventHandler(Application_PostAcquireRequestState);
    }

    void Application_PostAcquireRequestState(object source, EventArgs e)
    {
        if (HttpContext.Current.Handler is Page && HttpContext.Current.Session != null)
        {
            Page page = (Page)HttpContext.Current.Handler;

            Log(page.Title);
        }
    }
}

I can only see it being that page variable isn't initialized. 我只能看到页面变量未初始化。 Does HttpContext.Current.Handler actually contain a memory address or value? HttpContext.Current.Handler是否实际包含内存地址或值? If it's giving you a System.NullReferenceException then it is exactly that - Title, or the thing that is calling Title is null. 如果它给您一个System.NullReferenceException,那么它就是-Title,或者称Title为null的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM