I have created a custom error page when in production it will take user to correct HTTP status code page, rather than showing an ugly developer error page.
My code at the moment is as follows:
Startup.cs
public void Configure(IApplicationBuilder app, IHostEnvironment env, UserManager<SiteUser> user)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithRedirects("/Error/{0}");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSession();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
ErrorController:
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult HttpStatusCodeHandler(int statusCode)
{
switch (statusCode)
{
case 404:
ViewBag.ErrorMessage = "Uh oh, this page could not be found";
ViewBag.ErrorNumber = "404";
break;
case 405:
ViewBag.ErrorMessage = "Method not allowed. Contact administrator";
ViewBag.ErrorNumber = "405";
break;
case 401:
ViewBag.ErrorMessage = "You do not have acccess to this page. Please make sure you are logged in, or contact your administrator.";
ViewBag.ErrorNumber = "401";
break;
case 500:
ViewBag.ErrorMessage = "Internal Server Error. Please contact administrator.";
ViewBag.ErrorNumber = "500";
break;
case 403:
ViewBag.ErrorMessage = "Forbidden. Please contact administrator.";
ViewBag.ErrorNumber = "403";
break;
case 503:
ViewBag.ErrorMessage = "Service unavailable. Please contact administrator";
ViewBag.ErrorNumber = "503";
break;
case 504:
ViewBag.ErrorMessage = "Gateway Timeout. Please contact administrator";
ViewBag.ErrorNumber = "504";
break;
case 001:
ViewBag.ErrorMessage = "This link has expired.";
ViewBag.ErrorNumber = "Oh no!";
break;
}
return View("NotFound");
}
My problem is that whenever the web app throws a 500 error it doesn't bring user to this page but instead to this [![Wrong error page][1]][1]page...
Not sure what is going wrong because when i try to access a page that doesn't exist it takes the user to the correct page:
[![enter image description here][2]][2]
**EDIT In .net core do i need to use try catch if there is middle ware that handles the exception handling