我需要在global.asax文件中的一个函数,该函数只在用户输入页面url时调用一次。application_beginrequest在一个页面中会被调用50-60次(为了呈现一个页面,几个请求都会被发送到服务器)。
我想到了一个解决方案--我可以用global.asax编写函数,然后在其他页面的页面加载时调用它,但在这个解决方案中,我需要在每个页面中调用它。我更喜欢只在global.asax中做的事情
发布于 2010-03-25 16:03:16
您可以在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"))
{
// ...
}
}发布于 2010-03-25 16:05:01
你方法的另一个变化是创建一个调用你的全局方法的Page的子类,或者自己做这项工作,然后让你的所有页面扩展你的类,而不是直接扩展Page。
public class AcmePage : Page{
// override/subscribe to one of the page events
}
...
public class HomePage : AcmePage如果你只是想统计页面浏览量,可能值得看看可用的预置分析服务,比如免费的google analytics。周围还有其他的,比如CoreMetrics等。
https://stackoverflow.com/questions/2513834
复制相似问题