自定义错误仅在本地显示,但不在远程服务器上显示

时间:2014-10-28 18:09:01

标签: asp.net asp.net-mvc-4 iis

我为我的ASP.NET MVC应用程序创建了自定义错误,由于某些原因它们只显示在我的本地计算机上,但是当我在远程服务器上部署我的应用程序时,默认的IIS错误页面显示出来(404和500)我想知道为什么会这样。这是我的应用配置:

的web.config

<system.web>
    ...
        <customErrors mode="On" defaultRedirect="~/ServerError">
          <error statusCode="404" redirect="~/NotFound" />
        </customErrors>
    ...
</system.web>

路线配置

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Error - 404",
        "NotFound",
        new { controller = "Error", action = "NotFound" }
    );

    routes.MapRoute(
        "Error - 500",
        "ServerError",
        new { controller = "Error", action = "ServerError" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

错误控制器:

public class ErrorController : Controller
{
    [AllowAnonymous]
    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        return View();
    }

    [AllowAnonymous]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View();
    }
}

Global.asax中

protected void Application_Error(object sender, EventArgs e)
{
    Response.TrySkipIisCustomErrors = true;
}

本地计算机IIS配置和远程计算机看起来相似,但也许我错过了一些东西,我也必须正确配置它。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这解决了这个问题:

public class ErrorController : Controller
{
    [AllowAnonymous]
    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        Response.TrySkipIisCustomErrors = true;

        return View();
    }

    [AllowAnonymous]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        Response.TrySkipIisCustomErrors = true;

        return View();
    }
}