【问题标题】:maintain pageViews in global.asax (asp.net)在 global.asax (asp.net) 中维护 pageViews
【发布时间】:2010-03-25 07:59:51
【问题描述】:

我需要 global.asax 文件中的一个函数,当用户输入页面 url 时该函数只被调用一次。 application_beginrequest 在单个页面中被调用 50-60 次(因为要渲染一个页面,几个请求会发送到服务器。)

我想到了一个解决方案 - 我可以在 global.asax 中编写我的函数并在其他页面的页面加载时调用它,但在该解决方案中我需要在每个页面中调用它。我更喜欢只能在 global.asax 中完成的事情

【问题讨论】:

    标签: asp.net vb.net global-asax


    【解决方案1】:

    您可以在 Begin_Request 函数中测试请求 url:

    protected void Application_BeginRequest(object sender, EventArgs e) 
    {
        // Perform some test on the request url that suits your case:
        if (Request.Url.ToString().EndsWith(".aspx"))
        {
            // ...
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的方法的另一个变体是创建一个 Page 的子类,它调用您的全局方法,或者自己完成工作,然后让您的所有页面扩展您的类而不是直接扩展 Page。

      public class AcmePage : Page{
           // override/subscribe to one of the page events
      }
      ...
      public class HomePage : AcmePage
      

      如果您只是想计算页面浏览量,可能值得查看可用的预构建分析服务,例如免费的google analytics。还有其他的,比如 CoreMetrics 等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多