【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间
2014-05-28 17:03
776 查看
代码如下:
谢谢浏览!
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Web; using System.Web.Mvc; namespace MyPractise.Filters { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public class ActionSpeedProfilerAttribute : FilterAttribute, IActionFilter { private Stopwatch timer; public void OnActionExecuted(ActionExecutedContext filterContext) { timer.Stop(); if (filterContext.Exception == null) { string div = string.Format(@" <div style='position:absolute; left:0px; top:0px; width:280px; height:20px; text-align:center; background-color:#000000; color:#FFFFFF'> Action method took: {0} seconds </div>", timer.Elapsed.TotalSeconds.ToString("F6")); filterContext.HttpContext.Response.Write(div); } } public void OnActionExecuting(ActionExecutingContext filterContext) { timer = Stopwatch.StartNew(); } } }
谢谢浏览!
相关文章推荐
- C#.Net Mvc运营监控,计算方法/接口/action/页面执行时间
- ASP.NET MVC如何在Action中返回脚本并执行
- C#.Net Mvc运营监控,计算方法/接口/action/页面执行时间
- 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
- 如何让ASP.NET Web API的Action方法在希望的Culture下执行
- 在Asp.Net MVC中实现计算页面执行时间及简单流量统计
- 浅谈Asp.net Mvc之Action如何传多个参数的方法
- asp.net mvc OnActionExecuting 方法重写后的执行顺序
- ASP.NET MVC是如何运行的(4): Action的执行
- ASP.NET MVC中的ActionFilter是如何执行的?
- 在Asp.Net MVC中实现计算页面执行时间及简单流量统计
- ASP.NET MVC中的ActionFilter是如何执行的?
- ASP.NET MVC单元测试时如何对含有ModelState.IsValid的Action进行测试
- asp.net mvc 如何在执行完某任务后返回原来页面
- 从零开始学习 ASP.NET MVC 1.0 (三) Controller/Action 深入解析与应用实例
- [转] 获取页面执行时间的几种方法(asp.net)
- asp.net mvc 如何在执行完某任务后返回原来页面
- asp.net如何在用户关闭浏览器时执行Session_End方法
- 在ASP.NET MVC中如何应用多个相同类型的ValidationAttribute?