Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 对每个操作调用使用MVC Miniprofiler_Asp.net Mvc_Mvc Mini Profiler - Fatal编程技术网

Asp.net mvc 对每个操作调用使用MVC Miniprofiler

Asp.net mvc 对每个操作调用使用MVC Miniprofiler,asp.net-mvc,mvc-mini-profiler,Asp.net Mvc,Mvc Mini Profiler,我一直在试验伟大的工具Mvc 我不想在我所有的视图中乱放大量的Step命令,所以我想在每个动作调用中使用分析器。坏主意?这就是我迄今为止所尝试的: public abstract class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { var profiler

我一直在试验伟大的工具Mvc

我不想在我所有的视图中乱放大量的
Step
命令,所以我想在每个动作调用中使用分析器。坏主意?这就是我迄今为止所尝试的:

 public abstract class BaseController : Controller
 {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var profiler = MiniProfiler.Current;
            using (profiler.Step("Action: "+filterContext.ActionDescriptor.ActionName))
            {
                base.OnActionExecuting(filterContext);
            }
        }
}

但我不认为这是我想要的?我想我需要在
OnActionExecuting
上启动探查器,并在
OnResultExecuted
上停止它。考虑到探查器设计为与
using
语句一起使用,我如何做到这一点。

您可以定义全局操作筛选器:

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();
        }
    }
}
并在
Global.asax中注册:

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

就这些。

思考和使用ActionFilter是可以的。但是MVC仍然是一个ASP.NET应用程序。要在每个请求的开始和结束时启动和停止MiniProfiler,可以尝试Global.asax.cs文件中的应用程序事件

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="Believe2014">
//   http://believeblog.azurewebsites.net/post/miniprofiler--log4net
// </copyright>
// <summary>
//   The mvc application.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Mvc4Application
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    ///     The mvc application.
    /// </summary>
    public class MvcApplication : HttpApplication
    {
        /// <summary>
        ///     The application_ start.
        /// </summary>
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            Profiler.Initialize();
        }

        /// <summary>
        /// The event when the application acquires request state.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument..
        /// </param>
        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            Profiler.Start(HttpContext.Current);
        }

        /// <summary>
        /// This function is called by ASP .NET at the end of every http request.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event argument.
        /// </param>
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            Profiler.Stop();
        }
    }
}
//--------------------------------------------------------------------------------------------------------------------
// 
//   http://believeblog.azurewebsites.net/post/miniprofiler--log4net
// 
// 
//mvc应用程序。
// 
// --------------------------------------------------------------------------------------------------------------------
使用制度;
使用System.Web;
使用System.Web.Http;
使用System.Web.Mvc;
使用System.Web.Optimization;
使用System.Web.Routing;
命名空间MVC4应用程序
{
//注:有关启用IIS6或IIS7经典模式的说明,
//拜访http://go.microsoft.com/?LinkId=9394801
/// 
///mvc应用程序。
/// 
公共类MVC应用程序:HttpApplication
{
/// 
///应用程序将启动。
/// 
受保护的无效应用程序\u Start()
{
RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
Profiler.Initialize();
}
/// 
///应用程序获取请求状态时的事件。
/// 
/// 
///发送者。
/// 
/// 
///事件参数。。
/// 
受保护的无效应用程序状态(对象发送方、事件参数e)
{
Profiler.Start(HttpContext.Current);
}
/// 
///此函数由ASP.NET在每个http请求结束时调用。
/// 
/// 
///发送者。
/// 
/// 
///事件参数。
/// 
受保护的无效应用程序\u EndRequest(对象发送方,事件参数e)
{
Profiler.Stop();
}
}
}

如果您下载Mini Profiler的源代码,那么示例项目中有一个基本控制器类正是这样做的。Darin MVC 2是否有一个等效的方法?哈,实际上我只需要将[ProfileActions]添加到我的基本控制器中,这很简单。如果您想知道执行了哪个控制器和操作,请使用
var step=profiler.step(“控制器:”+filterContext.RouteData.Values[“控制器”].ToString()+“操作:”+filterContext.ActionDescriptor.ActionName),希望对某人有所帮助。我投了赞成票,因为Application\u AcquireRequestState最终成为我检查用户是否经过身份验证并有权使用/查看探查器的好地方,但这并不能严格解决OP的问题。这是请求的时间,而不是具体操作的时间。小细节,但可能很重要。有很多工具可以跟踪请求处理时间。但这种方法特别适用于跟踪经过身份验证的请求的处理时间。它讲述了每个用户及其等待时间的故事,而不仅仅是一般请求。