MVC 3错误页面不在Dev Server上显示

时间:2013-04-04 02:06:53

标签: asp.net-mvc asp.net-mvc-3 custom-error-pages

我为MVC应用创建了自定义视图。

我让错误冒泡到我的Globals.asax文件中的Application_Error处理程序,并执行重定向到呈现这些视图的错误控制器:

protected void Application_Error(object sender, EventArgs eventArgs)
{
    Exception exception = Server.GetLastError();
    Response.Clear();
    var httpException = exception as HttpException;

    Response.TrySkipIisCustomErrors = true;

    if (httpException != null)
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                Server.ClearError();
                Response.Redirect(UiConstants.PageNotFoundPage);
                break;
            case 500:
                if (httpException.Message.Equals(UiConstants.NoSessionStateExceptionMsg, StringComparison.Ordinal))
                {
                    Server.ClearError();
                    Response.Redirect(UiConstants.SessionNotAvailablePage);
                }
                else
                {
                    Server.ClearError();
                    Response.Redirect(UiConstants.ServerErrorPage);
                }
                break;
            default:
                Server.ClearError();
                Response.Redirect(UiConstants.ServerErrorPage);
                break;
        }
    }
    else
    {
        Server.ClearError();
        Response.Redirect(UiConstants.ServerErrorPage);
    }
}

在我的开发环境(VS Dev Web Server)中,这可以按设计工作。当我将其部署到测试服务器时,会显示库存IIS错误网页,而不是我的视图。

路径路径正确,即 servername / StaticContent / PageNotFound

任何想法如何解决这个问题?是否有一些IIS设置这样做?

1 个答案:

答案 0 :(得分:0)

我相信我已经确定了我的问题。在本地运行,基地址只有http://localhost:64133/。因此,我的所有重定向都在使用相对路径,例如http://localhost:64133/staticcontent/pagenotfound

然而,当我在开发环境中部署到我的开发服务器时,基本目录的路径略有不同,因为IIS应用程序位于名为WebRequest的文件夹中,该文件夹位于wwwroot内(在C:\ inetpub中)

这意味着应用程序的基本URL为http://servername/WebRequest/。因此,我的重定向将其发送到http://servername/staticcontent/pagenotfound,而不是http://servername/WebRequest/staticcontent/pagenotfound

对那些人抱歉。对于你们来说,这对你们来说非常困难,不知道那些环境缺陷。我想我们都可以从中学习。再次感谢。