> ## Content Index
> Fetch the complete content index at: https://blog.jsinh.in/llms.txt
> Use this file to discover other available public pages before exploring further.

# Stay DRY with PostSharp: Profile ASP.NET MVC application using MiniProfiler
- URL: https://blog.jsinh.in/stay-dry-with-postsharp-profile-aspnet-mvc-application-using-miniprofiler/
- Published: 2015-02-24T05:35:00.000Z
- Updated: 2026-07-07T15:07:42.000Z
- Author: Jsinh
- Tags: #Import 2026-10-04 19:16

There are so many productive tools and packages out there that can be used for profiling an ASP.NET MVC application. While back I wrote a quick intro about how [**Glimpse** can come handy profiling your web application](https://blog.jsinh.in/glimpse-for-debugging-and-profiling-asp-net-mvc/).

There is one more simplistic and quick way to start profiling load / execution time for your application routines using [**MiniProfiler**](https://github.com/MiniProfiler/dotnet?ref=blog.jsinh.in). This little package is prepared by folks at [Stackexchange and used for Stackoverflow.com and other sites](https://stackexchange.com/performance?ref=blog.jsinh.in).

[MiniProfile's homepage](http://miniprofiler.com/?ref=blog.jsinh.in) shows quick way to get started with using MiniProfiler in your application. Following code snippet is required to profile a piece of code (not to forget `MiniProfiler.Start()` and `MiniProfiler.Stop()`):

```
var profiler = MiniProfiler.Current;
using (profiler.Step("Set page title"))
{
    // Your code to profile
}

```

and you will see something similar to following when you load your application and run through profiled piece of code:

{<1>}![MiniProfiler getting started profile step demo](https://blob.jsinh.in/media/2015/02/24/SS01-MiniProfiler-PostSharp.png)

So if I need to profile the execution time taken for any / all controller actions of my application I need to wrap the block of code in my `Action` method of controller inside `using (profiler.Step("Your step name"))`.

> I need to wrap a code block as MiniProfiler.Step to profile it

is the part where AOP can come handy.

[**PostSharp**](https://www.postsharp.net/?ref=blog.jsinh.in) is a rich set of [ready-made Aspects to use out-of-the-box](https://www.postsharp.net/features?ref=blog.jsinh.in) and also extensible framework to create your own custom Aspects to write business centric, clean and more maintainable code. It will help you stay **DRY (Don't repeat yourself)** for the profiling part and also for other possible Aspects for your application.

This case scenario can be taken as simplistic example how PostSharp can help you achieve profiling in a cleaner and maintainable way!!

To wrap controller's Action method for profiling you can create a custom Aspect Attribute using [OnMethodBoundaryAspect](http://doc.postsharp.net/t%5Fpostsharp%5Faspects%5Fonmethodboundaryaspect?ref=blog.jsinh.in). The two main overridable methods were are interested for this example are `OnEntry(MethodExecutionArgs args)` and `OnExit(MethodExecutionArgs args)`. Code of this methods executes when the execution starts / stops for a particular method on which this Aspect is applied.

Basic `Profiler` `attribute` build using PostSharp would be something like this:

```
using System;
using PostSharp.Aspects;
using StackExchange.Profiling;

[Serializable]
[AttributeUsage(AttributeTargets.Method)]
public sealed class ProfilerAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        var profiler = MiniProfiler.Current;
        var profilerStep = profiler.Step(args.Method.Name);
        args.MethodExecutionTag = profilerStep;
        base.OnEntry(args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        var profilerStep = args.MethodExecutionTag as IDisposable;
        if (null != profilerStep)
        {
            profilerStep.Dispose();
            profilerStep = null;
        }

        base.OnExit(args);
    }
}

```

That is all. Now you can decorate your controller's action method (or any other method) with `[Profiler]` attribute to start profiling it. This attribute can only be used for decorating methods because of the attribute usage specified as `[AttributeUsage(AttributeTargets.Method)]`. You can modify `AttributeUsage` mode to use it on other targets.

I am capturing `profilerStep` instance to `MethodExecutionTag` (`OnMethodBoundaryAspect's` state object in simple words) in `OnEntry` and when `OnExit` is called I dispose the instance.

{<2>}![Before - After using PostSharp (OnMethodBoundaryAspect)](https://blob.jsinh.in/media/2015/02/24/SS02-Before-After-PostSharp-MiniProfiler.png)

**Note:** I am solely expressing my own opinions and sharing my experiences and I do not speak for or am not affiliated with MiniProfiler and / or PostSharp team.

Happy coding !!