Add Controller 專門處理錯誤請求。
public class ErrorsController : Controller { // // GET: /ErrorsController/ [HttpGet] public ActionResult Http404() { return View(); } [HttpGet] public ActionResult Http403() { return View(); } [HttpGet] public ActionResult General() { return View(); } }
在Global.asax 中添加應用程式錯誤的處理函示:
protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } IController errorsController = new ErrorsController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorsController.Execute(rc); }
撰寫錯誤畫面(404 Not Found)
@{ ViewBag.Title = "404 Not Found"; } <body style="color: #444; margin:0;font: normal 14px/20px Arial, Helvetica, sans-serif; height:100%; background-color: #fff;"> <div style="height:auto; min-height:100%; "> <div style="text-align: center; width:800px; margin-left: -400px; position:absolute; top: 30%; left:50%;"> <h1 style="margin:0; font-size:150px; line-height:150px; font-weight:bold;">404</h1> <h2 style="margin-top:20px;font-size: 30px;"> Not Found </h2> <p>The resource requested could not be found on this server!</p> </div> </div> </body>
文章標籤
全站熱搜