I googled and stackoverflowed this question a lot and its different variants, but I'm still confused if it's possible at all. I just want to add a custom header to all actions having specific attribute. Sounds simple? But it's not. I have just written following:
[AttributeUsage(AttributeTargets.Method)]
public class HelloWorldAttribute : ActionFilterAttribute
{
/// <inheritdoc />
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Headers["X-HelloWorld"] = string.Empty;
}
}
And it works fine for all requests except when they are forbidden by [Authorize]
on Controller
level.
I tried use to use this attribute for Controller
level and pass names of methods that have to add this header to it, but it doesn't work too. It seems that Authorize
has always a higher priority. And you can agree that it's ugly.
How can it be done?