mvc 找不到頁面 處理
如果有人亂key url 進網站 而找不到頁面時
mvc 有個系統錯誤畫面
如果要自行更改 參考
http://stackoverflow.com/questions/5226791/custom-error-pages-on-asp-net-mvc3/5229581#5229581
在 Global.asax 的 public class MvcApplication : System.Web.HttpApplication
內 加一個
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);
}
-----------------------------------
然後在 public class MvcApplication : System.Web.HttpApplication
同一層 加入
public class ErrorsController : Controller { public ActionResult General(Exception exception) { return Content("General failure", "text/plain"); } public ActionResult Http404() { //return Content("Not found", "text/plain"); return View(); //這是我加的 按右鍵 加入一個 view Http404.cshtml 裏頭就看你要怎麼搞 } public ActionResult Http403() { return Content("Forbidden", "text/plain"); } }
mvc 有個系統錯誤畫面
如果要自行更改 參考
http://stackoverflow.com/questions/5226791/custom-error-pages-on-asp-net-mvc3/5229581#5229581
在 Global.asax 的 public class MvcApplication : System.Web.HttpApplication
內 加一個
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);
}
-----------------------------------
然後在 public class MvcApplication : System.Web.HttpApplication
同一層 加入
public class ErrorsController : Controller { public ActionResult General(Exception exception) { return Content("General failure", "text/plain"); } public ActionResult Http404() { //return Content("Not found", "text/plain"); return View(); //這是我加的 按右鍵 加入一個 view Http404.cshtml 裏頭就看你要怎麼搞 } public ActionResult Http403() { return Content("Forbidden", "text/plain"); } }
留言
張貼留言