如何在.NET Asp MVC中开始页面请求时获取页面请求的初始时间戳?我想将Timestamp与Action的开头进行比较,然后再将其作为OnActionExecuted进行比较,并查看我的应用程序在开始响应“head”元素之前花费时间在管道中的哪个位置,它同时执行模型和正在处理数据请求。有什么建议吗?
答案 0 :(得分:2)
您订阅Global.asax中的Application_BeginRequest
,将当前时间戳放入HttpContext,然后放入OnActionExecuted
或从HttpContext中检索此时间戳的任何内容,计算新的时间戳并制作减法:
的Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
this.Context.Items["timestamp"] = DateTime.UtcNow;
}
OnActionExecuted:
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
DateTime initial = (DateTime)filterContext.HttpContext.Items["timestamp"];
DateTime current = DateTime.UtcNow;
long totalMilliseconds = (long)(current - initial).TotalMilliseconds;
...
}