Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created January 26, 2025 09:03
Show Gist options
  • Save dj-nitehawk/7cef738cf5c0d5a26981524df9228349 to your computer and use it in GitHub Desktop.
Save dj-nitehawk/7cef738cf5c0d5a26981524df9228349 to your computer and use it in GitHub Desktop.
Response interceptor example
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen();
app.Run();
sealed class ResponseInterceptor : IResponseInterceptor
{
public Task InterceptResponseAsync(object response,
int statusCode,
HttpContext ctx,
IReadOnlyCollection<ValidationFailure> failures,
CancellationToken ct)
{
ctx.Response.Headers.Append("x-some-header", "some value");
return Task.CompletedTask;
}
}
sealed class MyEndpoint : Ep.NoReq.Res<object>
{
public override void Configure()
{
Get("hello");
AllowAnonymous();
ResponseInterceptor(new ResponseInterceptor()); // can be set globally with endpoint configurator as well
}
public override async Task HandleAsync(CancellationToken c)
{
await SendInterceptedAsync(new { Messsge = "hello!" }, statusCode: 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment