ASP.NETMVC 404错误处理我在ASP.NETMVC中404 http错误处理程序(RC 5)我仍然得到标准404错误页面。我需要更改IIS中的某些内容吗?
3 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
<system.web> <customErrors mode="On" > <error statusCode="404" redirect="~/Errors/Error404" /> </customErrors></system.web>
<system.web> <customErrors mode="On" > <error statusCode="404" redirect="~/Static404.html" /> </customErrors></system.web>
POPMUISE
TA贡献1765条经验 获得超5个赞
protected void Application_EndRequest() { if (Context.Response.StatusCode == 404) { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "ErrorManager"; routeData.Values["action"] = "Fire404Error"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 404: routeData.Values["action"] = "Fire404Error"; break; } } // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; IController errormanagerController = new ErrorManagerController(); HttpContextWrapper wrapper = new HttpContextWrapper(Context); var rc = new RequestContext(wrapper, routeData); errormanagerController.Execute(rc); } }
public void Fire404Error(HttpException exception) { //you can place any other error handling code here throw new PageNotFoundException("page or resource"); }
public class MyBasePageController : Controller{ protected override void OnException(ExceptionContext filterContext) { filterContext.GetType(); filterContext.ExceptionHandled = true; this.View("ErrorManager", filterContext).ExecuteResult(this.ControllerContext); base.OnException(filterContext); }}
- 3 回答
- 0 关注
- 338 浏览
添加回答
举报
0/150
提交
取消