实例1.
/// <summary> /// 错误处理 /// 404 处理 /// </summary> protected void Application_Error(object sender, EventArgs e) { //判断请求方式 if (Request.HttpMethod == "GET") { // 获取错误 HttpException exception = Server.GetLastError() as HttpException; if (exception != null) { if (exception.GetHttpCode() == 404) { //输出指定的字符串 Response.Clear(); //Response.Write("code:404"); //输出指定controller下的内容 RouteData routeData = new RouteData(); routeData.Values.Add("controller", "ViewOne"); routeData.Values.Add("action", "TempTwo"); routeData.Values.Add("name", "zhangsan"); IController one = new ViewOneController(); one.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); /* * 如果不指定 结束输出的话,则还是会执行框架的自动处理(会清空自己输出的内容) * 如果指定结束的话,状态码是200,而不是400,所以需要自己手动指定状态码 */ Response.StatusCode = 404; Response.End(); Context.ClearError(); } } } }
实例2.
MVC中,有一个Filter可以捕捉错误,但是它的用法是利用Attribute来实现的,而且只能加在Controller和Action上,所以不能捕捉别出的错误
其实理论上所有的错误肯定产生于Controller中,但有2种情况下,就不会被捕捉了
1、页面不存在的时候,找不到对应的Controller,那没有任何Controller被执行,所以自然也不会捕捉到错误了
2、在 IAuthorizationFilter 下发生错误的时候,错误捕捉代码在IExceptionFilter中,而IAuthorizationFilter的优先权高于IExceptionFilter,所以也就捕捉不到了
protected void Application_Error(object sender, EventArgs e) { Exception exception = Server.GetLastError(); Response.Clear(); HttpException httpException = exception as HttpException; RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); if (httpException == null) { routeData.Values.Add("action", "Index"); } else //It's an Http Exception, Let's handle it. { switch (httpException.GetHttpCode()) { case 404: // Page not found. routeData.Values.Add("action", "HttpError404"); break; case 500: // Server error. routeData.Values.Add("action", "HttpError500"); break; // Here you can handle Views to other error codes. // I choose a General error template default: routeData.Values.Add("action", "General"); break; } } // Pass exception details to the target error View. routeData.Values.Add("error", exception.Message); // Clear the error on server. Server.ClearError(); // Call target Controller and pass the routeData. IController errorController = new WEB.Controllers.ErrorController(); errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); }
把这段代码放到 Global.asax 中,并且新建一个 Controller 叫做 Error
namespace MVC.Controllers { public class ErrorController : Controller { public ActionResult Index(string error) { ViewData["Title"] = "WebSite 网站内部错误"; ViewData["Description"] = error; return View("Index"); } public ActionResult HttpError404(string error) { ViewData["Title"] = "HTTP 404- 无法找到文件"; ViewData["Description"] = error; return View("Index"); } public ActionResult HttpError500(string error) { ViewData["Title"] = "HTTP 500 - 内部服务器错误"; ViewData["Description"] = error; return View("Index"); } public ActionResult General(string error) { ViewData["Title"] = "HTTP 发生错误"; ViewData["Description"] = error; return View("Index"); } } }
这样,就可以捕捉所有错误了。
但其实,这样也不是完美的,因为如果你参考了我第一个问题中,在IIS6下不修改IIS设置,运行了MVC,那当后缀名不是.aspx的时候,错误不会被捕捉
因为这时候输入的地址根本没有交给网站来处理,IIS直接抛出了错误,因为IIS认为这个后缀名不是你所能执行
特别标注:
webconfig 需要配置输出404的错误的详细信息
<system.webServer>
<httpErrors errorMode="Detailed" />
</system.webServer>