mvc中捕获错误

http://www.cnblogs.com/mylife_001/articles/1866100.html

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 WEB.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");
        }
    }
}

====================================MVC=========================================================

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();

            Response.Clear();


            HttpException httpException = exception as HttpException;
            if (Server.GetLastError().InnerException.ToString().IndexOf("failed to load") > 0 || Server.GetLastError().InnerException.ToString().IndexOf("File does not exist") > 0)
            {
                Response.Redirect( Request.ApplicationPath + "404error.aspx?aspxerrorpath=" + this.Request.GetRequestUrl());
            }
            Server.ClearError();
        }

你可能感兴趣的:(mvc,exception,String,object,application,action)