0

我正在使用以下代码将用户重定向到我的 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 发送的电子邮件中返回 page not found 错误。

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

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)

关于这个实现有什么问题的任何建议?

4

0 回答 0