Using MVC Miniprofiler for every action call

前端 未结 2 1094
别跟我提以往
别跟我提以往 2021-02-02 00:10

Iv been experimenting the great tool, Mvc MiniProfiler.

I don\'t want to litter all my view with lots of Step commands, so I am wanting to use the profiler

2条回答
  •  野的像风
    2021-02-02 00:50

    You could define a global action filter:

    public class ProfileActionsAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var profiler = MiniProfiler.Current;
            var step = profiler.Step("Action: " + filterContext.ActionDescriptor.ActionName);
            filterContext.HttpContext.Items["step"] = step;
        }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var step = filterContext.HttpContext.Items["step"] as IDisposable;
            if (step != null)
            {
                step.Dispose();
            }
        }
    }
    

    and register in Global.asax:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new ProfileActionsAttribute());
    }
    

    and that's pretty much all.

提交回复
热议问题