部署站点时MVC自定义错误视图问题

时间:2014-09-11 17:17:33

标签: visual-studio-2010 asp.net-mvc-4 windows-server-2008-r2

部署网站后,我发现了一些奇怪的问题。我已经指定了一个自定义错误控制器&使用以下内容在Global.asax.cs中查看:

    protected void Application_Error(object sender, EventArgs e)
    {
        var httpContext = ((MvcApplication)sender).Context;
        var currentController = " ";
        var currentAction = " ";
        var currentRouteData = RouteTable.Routes.GetRouteData(
            new HttpContextWrapper(httpContext));

        if (currentRouteData != null)
        {
            if (currentRouteData.Values["controller"] != null 
                && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
            {
                currentController = currentRouteData.Values["controller"].ToString();
            }

            if (currentRouteData.Values["action"] != null 
                && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
            {
                currentAction = currentRouteData.Values["action"].ToString();
            }
        }

        var ex = Server.GetLastError();
        var controller = new ErrorController();
        var routeData = new RouteData();
        var action = "Index";

        if (ex is HttpException)
        {
            var httpEx = ex as HttpException;

            switch (httpEx.GetHttpCode())
            {
                case 404:
                    action = "NotFound";
                    break;

                // others if any
            }
        }

        if (ex is NisSessionExpiredException)
        {
            action = "SessionExpired";
        }

        httpContext.ClearError();
        httpContext.Response.Clear();
        httpContext.Response.StatusCode = ex is HttpException 
            ? ((HttpException)ex).GetHttpCode() 
            : 500;
        httpContext.Response.TrySkipIisCustomErrors = true;

        routeData.Values["controller"] = "Error";
        routeData.Values["action"] = action;

        controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
        ((IController)controller).Execute(
            new RequestContext(new HttpContextWrapper(httpContext), routeData));
    }

在本地工作正常,但一旦部署在服务器上,当发生错误时它再次使用指定的控制器&行动,但它总是返回:

  

System.InvalidOperationException:视图'错误'或者它的主人是   找不到或没有视图引擎支持搜索的位置。该   搜索了以下位置:〜/ Views / Draft / Error.aspx   〜/ Views / Draft / Error.ascx~ / Views / Shared / Error.aspx   〜/ Views / Shared / Error.ascx~ / Views / Draft / Error.cshtml   〜/ Views / Draft / 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)   在   System.Web.Mvc.Async.AsyncControllerActionInvoker<> c__DisplayClass25.b__22(IAsyncResult的   asyncResult)at   System.Web.Mvc.Controller<> c__DisplayClass1d.b__18(IAsyncResult的   asyncResult)at   System.Web.Mvc.Async.AsyncResultWrapper<> c__DisplayClass4.b__3(IAsyncResult的   ar)在System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult   asyncResult)at   System.Web.Mvc.Async.AsyncResultWrapper<> c__DisplayClass4.b__3(IAsyncResult的   一只老鼠   System.Web.Mvc.MvcHandler<> c__DisplayClass8.b__3(IAsyncResult的   asyncResult)at   System.Web.Mvc.Async.AsyncResultWrapper<> c__DisplayClass4.b__3(IAsyncResult的   一只老鼠   System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,   布尔和放大器; completedSynchronously)

而不是预期的错误(正如您在上面看到的那样,错误视图应该为我提供抛出异常的详细信息以及调用堆栈)。在我的理解中,这个视图应该从未被调用过(并且它从不在本地调用)。

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,就像在web.config中添加以下行一样简单:

<customErrors mode="Off" />

事实证明,IIS的全局设置可能会强制要求使用这些设备或者其他类型,因为我之前没有指定过。