I'm currently working on an ASP.NET Core 2 MVC application. I try to figure out how global exception handling in Startup.cs
works.
So far so good, I can use the regular app.UseStatusCodePages()
middleware.
Nevertheless, when I try to use the app.UseStatusCodePagesWithReExecute
to show the HTTP status code on my view, I only get a standard HTTP 500 page and there is no redirect to my CustomError
action in my Error controller.
For demo purpose, I run my app in Production Environment, not in Development.
I throw my error in my ValuesController. The ValuesController looks like that:
public class ValuesController : Controller
{
public async Task<IActionResult> Details(int id)
{
throw new Exception("Crazy Error occured!");
}
}
My Startup.cs
looks like that:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Global Exception Handling, I run in Production mode
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else // I come into this branch
{
//app.UseExceptionHandler("/Error/Error");
// My problem starts here: I cannot redirect to my custom error method...
//there is only a standard http 500 screen
app.UseStatusCodePagesWithReExecute("/Error/CustomError/{0}");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Values}/{action=Index}/{id?}");
});
}
Finally, my ErrorController looks like that:
public class ErrorController : Controller
{
public IActionResult CustomError(string code)
{
// I only want to get here in debug, to get the code
// unfortunately it never happens :/
return Content("Error Test");
}
}
Unfortunately, I cannot redirect to my CustomError method and get the HTTP status code according to the exception.
There is only a standard chrome HTTP 500 page, thus nothing works.