Should this turn out to be useful for anyone, I\'ll gladly turn it into a community wiki thing.
I have some slow pages in an MVC3 app, and since little of the execution
There is already an action filter attribute in the MiniProfiler assembly that does the profiling for the actions. It is in the StackExchange.Profiling.MVCHelpers namespace and it's called ProfilingActionFilter. You can extend it to also profile your views.
It uses the same approach as described by @Dommer but instead of storing the IDisposable directly, it stores a Stack in the HttpContext.Current.Items. You can do the same for the views.
Here is the code for the action profiling:
///
/// This filter can be applied globally to hook up automatic action profiling
///
///
public class ProfilingActionFilter : ActionFilterAttribute
{
private const string stackKey = "ProfilingActionFilterStack";
///
/// Happens before the action starts running
///
///
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (MiniProfiler.Current != null)
{
Stack stack = HttpContext.Current.Items[(object) "ProfilingActionFilterStack"] as Stack;
if (stack == null)
{
stack = new Stack();
HttpContext.Current.Items[(object) "ProfilingActionFilterStack"] = (object) stack;
}
MiniProfiler current = MiniProfiler.Current;
if (current != null)
{
RouteValueDictionary dataTokens = filterContext.RouteData.DataTokens;
string str1 = !dataTokens.ContainsKey("area") || string.IsNullOrEmpty(dataTokens["area"].ToString()) ? "" : (string) dataTokens["area"] + (object) ".";
string str2 = Enumerable.Last((IEnumerable) filterContext.Controller.ToString().Split(new char[1] { '.' })) + ".";
string actionName = filterContext.ActionDescriptor.ActionName;
stack.Push(MiniProfilerExtensions.Step(current, "Controller: " + str1 + str2 + actionName, ProfileLevel.Info));
}
}
base.OnActionExecuting(filterContext);
}
///
/// Happens after the action executes
///
///
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
Stack stack = HttpContext.Current.Items[(object) "ProfilingActionFilterStack"] as Stack;
if (stack == null || stack.Count <= 0) return;
stack.Pop().Dispose();
}
}
Hope this help.