Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 049ade7b7a
Fetching contributors…

Cannot retrieve contributors at this time

68 lines (58 sloc) 2.211 kb
#if ASP_NET_MVC3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
namespace StackExchange.Profiling.MVCHelpers
{
/// <summary>
/// This filter can be applied globally to hook up automatic action profiling
/// </summary>
public class ProfilingActionFilter : ActionFilterAttribute
{
const string stackKey = "ProfilingActionFilterStack";
/// <summary>
/// Happens before the action starts running
/// </summary>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var mp = MiniProfiler.Current;
if (mp != null)
{
var stack = HttpContext.Current.Items[stackKey] as Stack<IDisposable>;
if (stack == null)
{
stack = new Stack<IDisposable>();
HttpContext.Current.Items[stackKey] = stack;
}
var profiler = MiniProfiler.Current;
if (profiler != null)
{
var tokens = filterContext.RouteData.DataTokens;
string area = tokens.ContainsKey("area") && !string.IsNullOrEmpty(tokens["area"].ToString()) ?
tokens["area"] + "." :
"";
string controller = filterContext.Controller.ToString().Split('.').Last() + ".";
string action = filterContext.ActionDescriptor.ActionName;
stack.Push(profiler.Step("Controller: " + area + controller + action));
}
}
base.OnActionExecuting(filterContext);
}
/// <summary>
/// Happens after the action executes
/// </summary>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var stack = HttpContext.Current.Items[stackKey] as Stack<IDisposable>;
if (stack != null && stack.Count > 0)
{
stack.Pop().Dispose();
}
}
}
}
#endif
Jump to Line
Something went wrong with that request. Please try again.