I have a .Net Core Entity Framework app that allows the user to see lists of engine production and also edit certain things.
If there are no errors, everything looks great.
But when I have an error, it uses an ugly view instead of my normal view.
Here is an example of a controller that looks great when there are no errors, but if there are errors, it's just black text on a white background.
Is there anyway to make it fit in with the rest of my web app?
Thanks!
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,EngineName,FactoryName")] ProductionRound productionRound)
{
if (ModelState.IsValid)
{
try
{
_context.Update(productionRound);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProductionRoundExists(productionRound.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(productionRound);
}
CodePudding user response:
You can custom an error page and use UseStatusCodePagesWithReExecute
and UseExceptionHandler
middleware to execute the error page like below:
View(StatusCode.cshtml
located in Views/Shared
folder):
<div >
<h1 >
<span>This is a user-friendly error page :)</span>
</h1>
Error code: @ViewData["code"]
</div>
Controller:
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult StatusCodeError(int statusCode)
{
ViewData["code"] = statusCode;
return View("StatusCode");
}
}
Startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
app.UseExceptionHandler("/Error/500");
//other middleware...
}