C
C#3mo ago
Iago

Middleware Error

Guys, I created a middleware to handle exceptions and added it to "Program.cs" but it is giving this error when running. .NET 8 Error:
"System.InvalidOperationException: 'An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });' or configure to generate a 'ProblemDetails' response in 'service.AddProblemDetails()'.'"
"System.InvalidOperationException: 'An error occurred when configuring the exception handler middleware. Either the 'ExceptionHandlingPath' or the 'ExceptionHandler' property must be set in 'UseExceptionHandler()'. Alternatively, set one of the aforementioned properties in 'Startup.ConfigureServices' as follows: 'services.AddExceptionHandler(options => { ... });' or configure to generate a 'ProblemDetails' response in 'service.AddProblemDetails()'.'"
My Code:
app.UseMiddleware<ExceptionHandlerMiddleware>();
app.UseMiddleware<ExceptionHandlerMiddleware>();
2 Replies
Kringe
Kringe3mo ago
You need to set the ExceptionHandlingPath so like the error says
app.UseExceptionHandler("/error");
app.UseExceptionHandler("/error");
or
builder.Services.AddExceptionHandler(options => options.ExceptionHandlingPath = "/error");
app.UseMiddleware<ExceptionHandlerMiddleware>();
builder.Services.AddExceptionHandler(options => options.ExceptionHandlingPath = "/error");
app.UseMiddleware<ExceptionHandlerMiddleware>();
or is this custom middleware and you accidentally using the one from microsoft
Iago
Iago3mo ago
true, I was importing from an asp.netcore class