Skip to content

Instantly share code, notes, and snippets.

@hartmannr76
Created April 16, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartmannr76/07e474cc58d7cae0d0b6a1b2d53df65c to your computer and use it in GitHub Desktop.
Save hartmannr76/07e474cc58d7cae0d0b6a1b2d53df65c to your computer and use it in GitHub Desktop.
.NET Core Action Filter With Controller Context
public class ControllerContextFilter : IAsyncActionFilter
{
public ControllerContextFilter() { }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// Sample captures response time for controller/actions
var sw = new Stopwatch();
sw.Start();
await next();
sw.Stop();
var ad = context.ActionDescriptor as ControllerActionDescriptor;
Console.Out.WriteLine(ad?.ActionName);
Console.Out.WriteLine(ad?.ControllerName);
Console.Out.WriteLine(context?.HttpContext.Request.Method);
Console.Out.WriteLine(sw.ElapsedMilliseconds());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment