ASP.NET MVC中的错误-友好的处理方法

转自:http://blog.csdn.net/lizhao1226/article/details/6367400


无法找到资源。

说明: HTTP 404。您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的 URL: /sdf


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1

//--------------------------------------------------------------------------------------------------------------------------------------------

MVC中,有一个Filter可以捕捉错误,但是它的用法是利用Attribute来实现的,而且只能加在Controller和Action上,所以不能捕捉别出的错误

其实理论上所有的错误肯定产生于Controller中,但有2种情况下,就不会被捕捉了

1、页面不存在的时候,找不到对应的Controller,那没有任何Controller被执行,所以自然也不会捕捉到错误了

2、在 IAuthorizationFilter 下发生错误的时候,错误捕捉代码在IExceptionFilter中,而IAuthorizationFilter的优先权高于IExceptionFilter,所以也就捕捉不到了

[c-sharp]view plaincopy

  1. protected void Application_Error(object sender, EventArgs e)
  2. {
  3. Exception exception = Server.GetLastError();
  4. Response.Clear();
  5. HttpException httpException = exception as HttpException;
  6. RouteData routeData = new RouteData();
  7. routeData.Values.Add("controller", "Error");
  8. if (httpException == null)
  9. {
  10. routeData.Values.Add("action", "Index");
  11. }
  12. else //It's an Http Exception, Let's handle it.
  13. {
  14. switch (httpException.GetHttpCode())
  15. {
  16. case 404:
  17. // Page not found.
  18. routeData.Values.Add("action", "HttpError404");
  19. break;
  20. case 500:
  21. // Server error.
  22. routeData.Values.Add("action", "HttpError500");
  23. break;
  24. // Here you can handle Views to other error codes.
  25. // I choose a General error template
  26. default:
  27. routeData.Values.Add("action", "General");
  28. break;
  29. }
  30. }
  31. // Pass exception details to the target error View.
  32. routeData.Values.Add("error", exception.Message);
  33. // Clear the error on server.
  34. Server.ClearError();
  35. // Call target Controller and pass the routeData.
  36. IController errorController = new WEB.Controllers.ErrorController();
  37. errorController.Execute(new RequestContext(
  38. new HttpContextWrapper(Context), routeData));
  39. }

把这段代码放到 Global.asax 中,并且新建一个 Controller 叫做 Error

[c-sharp]view plaincopy

  1. namespace MVC.Controllers
  2. {
  3. public class ErrorController : Controller
  4. {
  5. public ActionResult Index(string error)
  6. {
  7. ViewData["Title"] = "WebSite 网站内部错误";
  8. ViewData["Description"] = error;
  9. return View("Index");
  10. }
  11. public ActionResult HttpError404(string error)
  12. {
  13. ViewData["Title"] = "HTTP 404- 无法找到文件";
  14. ViewData["Description"] = error;
  15. return View("Index");
  16. }
  17. public ActionResult HttpError500(string error)
  18. {
  19. ViewData["Title"] = "HTTP 500 - 内部服务器错误";
  20. ViewData["Description"] = error;
  21. return View("Index");
  22. }
  23. public ActionResult General(string error)
  24. {
  25. ViewData["Title"] = "HTTP 发生错误";
  26. ViewData["Description"] = error;
  27. return View("Index");
  28. }
  29. }
  30. }

这样,就可以捕捉所有错误了。

但其实,这样也不是完美的,因为如果你参考了我第一个问题中,在IIS6下不修改IIS设置,运行了MVC,那当后缀名不是.aspx的时候,错误不会被捕捉

因为这时候输入的地址根本没有交给网站来处理,IIS直接抛出了错误,因为IIS认为这个后缀名不是你所能执行的.