Unscheduled expectations are not routed to my custom error page.

My understanding is that if you added app.UseExceptionHandler(); and gave him the way ASP.Net should load this page when there is an error that didn’t get into the code, but in my case I still get the normal "Http 500 Internal Server Error". I can take the path that I give UseExceptionHandler() , and put it directly in my browser, and it loads the page so that I know that the path and page are working. Am I lacking in understanding how this works, is it broken, or am I something wrong?

Startup.cs:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} app.UseExceptionHandler("/Error/ServerError"); app.UseIISPlatformHandler(); app.UseSession(); app.UseStaticFiles(); app.UseStatusCodePagesWithReExecute("/Error/PageNotFound"); app.UseMvc(); } 

Error Page Code:

 //Get: "/Error/ServerError" [Route("Error/ServerError")] public IActionResult ServerError() { return View(); //View is inside the Shared folder. } 

View error page:

 <p> @ViewData["ErrorMessage"] </p> 

Please note that the "Page not found" errors are redirected to /Error/PageNotFound without problems, these are just other errors.

EDIT: As a test, I copied the line from UseStatusCodePagesWithReExecute to UseExceptionHandler , but still get a 500 common error page.

Edit 2: I have to note that I test this error in two ways. The first is a simple action throw new DivideByZeroException(); and the other in calling LINQ to Entities to a database that was offline offline (and as such throws a SqlException ). Both methods simply return the default HTTP 500 Internal Server Error , not my custom error.

+6
source share
1 answer

Initially, I thought the use of the UseXXX functions was important, but my testing turned out to be wrong.

The only way I was able to get your example to work was to change the error page code to the following:

 [Route("Error/ServerError")] public IActionResult ServerError() { return View("Error/ServerError"); } 

It is assumed that the following exists: [ProjectDir] \ Views \ Shared \ Error \ ServerError.cshtml

If you set the view name to just “ServerError” and you see that your view is in [ProjectDir] \ Views \ Shared \ ServerError.cshtml, it looks like a bug in Microsoft.AspNet.Diagnostics or Microsoft. AspNet.Mvc package.

When an erroneous request is made, the following is logged:

 info: Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker[1] Executing action method WebApplication2.Controllers.ErrorsController.Get with arguments () - ModelState is Valid' fail: Microsoft.AspNet.Mvc.ViewFeatures.ViewResultExecutor[0] The view 'ServerError' was not found. Searched locations: /Views/Errors/ServerError.cshtml, /Views/Shared/ServerError.cshtml fail: Microsoft.AspNet.Diagnostics.ExceptionHandlerMiddleware[0] An exception was thrown attempting to execute the error handler. System.InvalidOperationException: The view 'ServerError' was not found. The following locations were searched: /Views/Errors/ServerError.cshtml /Views/Shared/ServerError.cshtml. 

The log clearly indicates that an attempt was made to find a view in /Views/Shared/ServerError.cshtml. However, the request does not work and uses the default error handler.

This issue has been reported in the gathub aspnet / Diagnostics repository here .

+1
source

All Articles