How to retrieve the original endpoint from within a exception handler? #60765
-
Is there a way to retrieve the endpoint from which the error/exception originates? I tried using, for exemple, this snippet: app.UseExceptionHandler(exceptionHandlerApp => exceptionHandlerApp.Run(context =>
{
var endpoint = context.GetEndpoint();
return Results.Problem().ExecuteAsync(context);
})); But app.UseExceptionHandler("/error");
app.Map("/error", (HttpContext context) =>
{
var endpoint = context.GetEndpoint();
}); But Debugging the code, I found out that It was introduced here: #52652 (comment) It seems like a internal thing, so I'll avoid using it. I have all my endpoints following this pattern: RouteGroupBuilder group = app
.MapGroup("/v1")
.WithOpenApi()
.AllowAnonymous()
.WithTags("Transactions");
group
.MapPost("/transactions", Create);
// other endpoints in the group I've referred to:
To add context, I need this to migrate our exception handler from Fast Endpoints to Minimal API, more details here: FastEndpoints/FastEndpoints#875; I considered using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To achieve something similar to the internal app.Use((context, next) =>
{
if (context.GetEndpoint() is RouteEndpoint endpoint &&
endpoint.RoutePattern.PathSegments[0].Parts[0] is RoutePatternLiteralPart { Content: "api" })
{
context.Items.Add("OriginalEndpoint", endpoint);
}
return next(context);
}); |
Beta Was this translation helpful? Give feedback.
To achieve something similar to the internal
__OriginalEndpoint
I created a middleware that checks for a specific part of the route pattern, since it has a predictable prefix: