Get HttpStatus code from IActionFilter in .Net Core 2.0

Quite an interesting question. Action filters are executed just after the action itself. The problem is that IActionResult returned by the action is not yet executed at this stage. You could check it by returning your custom implementation of IActionResult and checking that its ExecuteResultAsync method is executed after OnActionExecuted() of the action filter .

Since response is populated by IActionResult (including status code), you shouldn't expect that Response.StatusCode will be already set in action filter.

To solve this problem you should execute your logic later in ASP.Net Core pipeline, action filter is just not a proper place for it. You could add custom middleware in request pipeline (Startup.Configure() method):

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Use(async (context, next) =>
    {
        await next.Invoke();
        var statusCode = context.Response.StatusCode;
        // ...
    });

    app.UseMvc();
}

Make sure you add it before call to app.UseMvc(). You could wrap delegate logic to separate class if required.