Created
January 26, 2025 09:03
-
-
Save dj-nitehawk/7cef738cf5c0d5a26981524df9228349 to your computer and use it in GitHub Desktop.
Response interceptor example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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