asp.net MVC4中的错误处理 - IIS7.5

时间:2012-04-18 07:25:30

标签: error-handling elmah asp.net-mvc-4

我使用以下代码将用户重定向到我的asp.net mvc4应用程序中的错误页面,并使用ELMAH将错误发送为电子邮件:

protected void Application_Error()
        {
            Exception lastException = Server.GetLastError();
            //add user specific details
            lastException.Data["MachineName"] = Environment.MachineName;
            lastException.Data["ClientIP"] = Request.UserHostAddress.ToString();
            if (Request.ServerVariables["REMOTE_HOST"] != null)
            {
                System.Net.IPHostEntry host = new System.Net.IPHostEntry();
                host = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]);
                lastException.Data["ClientMachineName"] = host.HostName;
            }

            IController errorController;

            HttpException httpException = lastException as HttpException;
            RouteData routeData = new RouteData();
            //Try catch the error, as NLog throws error in Release mode
            System.Diagnostics.EventLog _eventLog = new System.Diagnostics.EventLog("Application", Environment.MachineName, ConfigurationManager.AppSettings[Constants.EventLogSource]);
            _eventLog.WriteEntry(lastException.ToString(), System.Diagnostics.EventLogEntryType.Error);

            //Possible reason for null is the the server error may not be a proper http exception
            if (httpException == null)
            {
                //Call target Controller
                errorController = new ErrorController();
                routeData.Values.Add("controller","Error");
                routeData.Values.Add("action", "Index");
                Response.Clear();
            }
            else
            //It's an Http Exception, Let's handle it.   
            {
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        //Page not found.
                        //Call target Controller
                        errorController = new UserController();
                        routeData.Values.Add("controller", "User");
                        routeData.Values.Add("action", "PageNotFound");
                        break;
                    case 302:
                        //Page Temporarily Moved
                        //Call target Controller
                        errorController = new UserController();
                        routeData.Values.Add("controller", "User");
                        routeData.Values.Add("action", "PageNotFound");
                        break;
                    case 500:
                        //Server error.
                        //Call target Controller
                        errorController = new ErrorController();
                        routeData.Values.Add("controller","Error");
                        routeData.Values.Add("action", "Index");
                        break;
                    //Here you can handle Views to other error codes.           
                    //I choose to redirect to Signin
                    default:
                        //Call target Controller
                        errorController = new ErrorController();
                        routeData.Values.Add("controller","Error");
                        routeData.Values.Add("action", "Index");
                        break;
                }
            }
            //Pass exception details to the target error View.  
            routeData.Values.Add("error", lastException);
            //Clear the error on server.  
            Server.ClearError();
            //Call target Controller and pass the routeData.  
            errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }

它在LOCAL中完美地返回错误,但是当从服务器(Windows Server 2008 R2,IIS 7.5)中,它在ELMAH发送的电子邮件中返回找不到页面错误。

本地错误:(这是完美的)

System.Web.HttpCompileException: c:\Projects\MVC4\Views\Home\Demo.cshtml(15): error CS0103: The name 'ResolveUrl' does not exist in the current context

Generated: Wed, 18 Apr 2012 07:21:05 GMT

服务器错误:(这不正确)

System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Error.aspx ~/Views/Home/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/Home/Error.cshtml ~/Views/Home/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml

Generated: Wed, 18 Apr 2012 07:19:11 GMT

System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Error.aspx
~/Views/Home/Error.ascx
~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx
~/Views/Home/Error.cshtml
~/Views/Home/Error.vbhtml
~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml
   at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)

有关此实施的错误的任何建议吗?

0 个答案:

没有答案