在ASP.NET Core 3.0中,可以通过以下步骤自定义错误页面:
public class ErrorController : Controller
{
[Route("Error/{statusCode}")]
public IActionResult HttpStatusCodeHandler(int statusCode)
{
var statusCodeResult = HttpContext.Features.Get();
switch (statusCode)
{
case 404:
ViewBag.ErrorMessage = "Sorry, the resource you requested could not be found.";
break;
// add more cases for other status codes if needed
}
return View("NotFound");
}
}
@{
ViewData["Title"] = "Page Not Found";
}
Page Not Found
@ViewBag.ErrorMessage
app.UseStatusCodePagesWithReExecute("/Error/{0}");
现在,当出现404错误时,将显示自定义错误页面。
请注意,如果您希望自定义其他错误页面,可以在"ErrorController"中添加更多的action方法和对应的视图。并且可以使用不同的状态码来区分不同的错误页面。