Chau Chun
Chau Chun

Reputation: 105

Handle redirect function when ASP.NET Core Web MVC error in application startup ( program.cs )

I want to handle my net core website able to catch the error and redirect to the error page or other sites if the middleware error or web application build fails at startup.

enter image description here

Methods I have tried

1.Add custom ErrorHandlerMiddleware and redirect page in trycatch,but redirect cause infinite loop and getting error "ERR_TOO_MANY_REDIRECTS" enter image description here

2.Use "UseExceptionHandler" and errorHandlingPath to Error Controller return and redirect to target page,but getting http error 500

Is it possible to handle redirect to other pages when net core web startup error?

Upvotes: 0

Views: 460

Answers (1)

Ruikai Feng
Ruikai Feng

Reputation: 11611

If web application build failed,You may not be able to reach the Error Page

If you want to handle the errors may cause webapplication build failure,you'd better directicty write the response with lambda

app.UseExceptionHandler(x => x.Run(async context=> {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync("SomeError");
            }));

Upvotes: 1

Related Questions