Skip to content

Instantly share code, notes, and snippets.

@Ilchert
Last active October 5, 2022 09:54
Show Gist options
  • Save Ilchert/165df36ff6abd92223424575af8f6ad6 to your computer and use it in GitHub Desktop.
Save Ilchert/165df36ff6abd92223424575af8f6ad6 to your computer and use it in GitHub Desktop.
Aspnet minimal api filters
using Microsoft.AspNetCore.TestHost;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseTestServer();
var app = builder.Build();
var group = app.MapGroup("/");
group.AddEndpointFilter(async (efiContext, next) =>
{
var result = await next(efiContext);
if (result is IValueHttpResult valueResult)
return new { data = valueResult.Value };
if (result is IResult)
throw new Exception("ХЗ чо делать");
return new { data = result };
});
group.MapGet("/", () => Results.Ok(new { Name = "name" }));
await app.StartAsync();
var client = app.GetTestClient();
var response = await client.GetStringAsync("");
Console.WriteLine(response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment