在使用Asp.Net Core Mvc時 404處理整理以下html
1、自帶404狀態處理post
1.控制器視圖子彈404視圖 NotFoundResult,NotFoundObjectResult網站
// // 摘要: // Creates an Microsoft.AspNetCore.Mvc.NotFoundObjectResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound // response. // // 返回結果: // The created Microsoft.AspNetCore.Mvc.NotFoundObjectResult for the response. [NonAction] public virtual NotFoundObjectResult NotFound(object value); // // 摘要: // Creates an Microsoft.AspNetCore.Mvc.NotFoundResult that produces a Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound // response. // // 返回結果: // The created Microsoft.AspNetCore.Mvc.NotFoundResult for the response. [NonAction] public virtual NotFoundResult NotFound();
2.當前操做返回404狀態,或者返回404的一句話提示。url
2、自定義404頁面顯示spa
在網站中,爲了加強提早,一般使用自定義404頁面code
1.自定義404視圖,在控制器中返回htm
/// <summary> /// 定義404視圖 /// </summary> public class NotFoundViewResult : ViewResult { public NotFoundViewResult(string viewName) { ViewName = viewName; StatusCode = (int)HttpStatusCode.NotFound; } }
2.在控制器中返回使用blog
public IActionResult Index() { //返回404頁面 return new NotFoundViewResult("~/views/Error/code_404.cshtml"); return View(); }
3.呈現結果:get
3、更多錯誤處理string
更多: