Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Created January 30, 2025 13:16
Show Gist options
  • Save dj-nitehawk/be15f1125cafc4ddd1c233eca26c0a8a to your computer and use it in GitHub Desktop.
Save dj-nitehawk/be15f1125cafc4ddd1c233eca26c0a8a to your computer and use it in GitHub Desktop.
Global response modifier
var bld = WebApplication.CreateBuilder(args);
bld.Services
.SwaggerDocument()
.AddFastEndpoints();
var app = bld.Build();
app.UseFastEndpoints(
c => c.Endpoints.GlobalResponseModifier
= (ctx, content) =>
{
ctx.Response.Headers.Append("x-some-header", content as string);
})
.UseSwaggerGen();
app.Run();
sealed class SomeEndpoint : Ep.NoReq.Res<string>
{
public override void Configure()
{
Get("some-endpoint");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
await SendAsync("hello world!");
}
}
@dj-nitehawk
Copy link
Author

image

@tmorejon
Copy link

tmorejon commented Mar 2, 2025

Is it possible to substitute the content object for another object before sending the response? Maybe for encrypting the payload? Example.

var decryptedResponseStr = JsonSerializer.Serialize(content);
var encryptedResponseStr = AES.Encrypt(decryptedResponseStr, "secretKey");
content = new EncryptedBody { Data = encryptedResponseStr };

@tmorejon
Copy link

tmorejon commented Mar 2, 2025

That worked perfectly. Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment