I have an asp .net core web api where I need to set some headers in the response. This based on the body of the response. (calculate md5)
I would like to do this in a custom middleware so that every request will have the same response headers.
I tried the following setup:
First I have a controller with a simple endpoint:
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
[Route("Test")]
public IActionResult DoTest()
{
var result = new { someProp = "somePropValue" };
return Ok(result);
}
}
I added a custom middleware class:
public class CustomResponseHandler
{
private readonly RequestDelegate _next;
public CustomResponseHandler(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await _next(context);
context.Response.Headers.Add("foo", "fii");
}
}
In startup I added this middleware using the Configure method:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseMiddleware<CustomResponseHandler>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
When I run this, the context.Response.Headers.Add("foo", "fii"); line does get executed.
It also executes after returning the result from the endpoint which is perfect.
However, when I look at the response, the header "foo" is still missing.
Is there a way to achieve this.
I noticed I do get the header when I add it before returning the result in the endpoint:
public IActionResult DoTest()
{
var result = new { someProp = "somePropValue" };
Response.Headers.Add("foo", "fii");
return Ok(result);
}