【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间
2014-05-28 17:03 音乐让我说 阅读(561) 评论(0) 收藏 举报代码如下:
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(); } } }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】开源 Linux 服务器运维管理面板 1Panel V2 版本正式发布
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2012-05-28 (转)HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
2012-05-28 ASP.NET 中 Web Service 的使用简单案例