我有一個 .Net Core Entity Framework 應用程式,它允許用戶查看引擎生產串列并編輯某些內容。
如果沒有錯誤,一切看起來都很棒。
但是當我出現錯誤時,它會使用丑陋的視圖而不是我的正常視圖。
這是一個在沒有錯誤時看起來很棒的控制器示例,但如果有錯誤,它只是白底黑字。
有沒有辦法讓它適合我的網路應用程式的其余部分?
謝謝!
[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);
}
uj5u.com熱心網友回復:
您可以自定義錯誤頁,使用UseStatusCodePagesWithReExecute和UseExceptionHandler中間件執行錯誤頁面如下圖所示:
查看(StatusCode.cshtml位于Views/Shared檔案夾中):
<div >
<h1 >
<span>This is a user-friendly error page :)</span>
</h1>
Error code: @ViewData["code"]
</div>
控制器:
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult StatusCodeError(int statusCode)
{
ViewData["code"] = statusCode;
return View("StatusCode");
}
}
啟動.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
app.UseExceptionHandler("/Error/500");
//other middleware...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371324.html
