zoukankan      html  css  js  c++  java
  • 【MVC 过滤器的应用】ASP.NET MVC 如何统计 Action 方法的执行时间

    代码如下:

    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;
                         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语言
    [C语言
    [C语言
    [C语言
    [C语言
    [C语言
    [iOS]超详细Apache服务器的配置(10.10系统)
    IOS优秀博客
    「C」 数组、字符串、指针
    103.Binary Tree Zigzag Level Order Traversal(层序遍历)
  • 原文地址:https://www.cnblogs.com/Music/p/to-stat-action-execute-times-with-asp-net-mvc-filter.html
Copyright © 2011-2022 走看看