【问题标题】:Can't redirect to custom error pages无法重定向到自定义错误页面
【发布时间】:2014-03-07 18:18:09
【问题描述】:

出现错误时,我正在尝试重定向到自定义页面。于是我在Web.Config添加了如下代码

    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Error/Error500.cshtml">
          <error statusCode="404" redirect="~/Views/Error/Error404"/>
          <error statusCode="500" redirect="~/Views/Error/Error500"/>
    </customErrors>
  </system.web>
    <system.webServer>
        <httpErrors errorMode="Custom" existingResponse="Replace">
          <remove statusCode="404" subStatusCode="-1" />
          <error statusCode="404" path="~/Views/Error/Error404" responseMode="ExecuteURL" />
          <remove statusCode="500" subStatusCode="-1" />
          <error statusCode="500" path="~/Views/Error/Error500" responseMode="ExecuteURL" />
        </httpErrors>

但是当我尝试使用 localhost:23920/aFakeURl 时,它会将我重定向到一个空白页面,并且无法到达我的 ErrorController

如果我尝试 localhost:23920/Error/Error404 它会进入我的控制器

// GET: /Error/Error404
public ActionResult Error404()
{
   Response.StatusCode = 404;
   return View();
}

然后它返回一个statusCode 404 并且 IIS 不知道如何处理它,它给了我一个空白页。所以我很确定问题出在Web.Config 中的路径。

我试过了

  • ~/Views/Error/Error404"
  • ~/Views/Error/Error404.cshtml"
  • /Views/Error/Error404"
  • /Views/Error/Error404.cshtml"

值得一提的是,当路径没有 ~ 时,它会返回运行时异常而不是空白页。

所以我有两个问题。

  1. Web.Config 的正确写法是什么?
  2. 我应该像这样返回正确的状态码吗 Response.StatusCode = 404;ErrorController?

谢谢

我不知道是否值得一提,但我使用Elmah 进行错误处理和记录。不知道它是否与这个问题有关,但我在他们的文档中读到它应该与mode="On" 一起使用。有没有更好的方法来处理这一切?

编辑

现在我用这个

    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/Error/Error500">
      <error statusCode="404" redirect="/Error/Error404"/>
      <error statusCode="500" redirect="/Error/Error500"/>
    </customErrors>
  </system.web>
  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/Error/Error500" responseMode="ExecuteURL" />
    </httpErrors>

但是当我输入错误的 url 时它总是返回错误 500。

例外是System.Web.HttpException: The controller for path '/aBadUrllllll' was not found or does not implement IController.

为什么不返回 404 错误?

我是否必须更改 Route.config 中的某些内容?

【问题讨论】:

  • 尝试 defaultRedirect = "~/Error/Error404"。我认为应该重定向到动作,而不是物理路径
  • customError 也一样吗?我还有一个空白页:(
  • 您是否也将“redirect="~/Views/Error/Error404”更新为操作?
  • 是的。 “~/Error/Error404”的所有内容
  • 它可能与redirectMode="ResponseRewrite"有关。 ben.onfabrik.com/posts/aspnet-mvc-custom-error-pages

标签: c# asp.net-mvc iis asp.net-mvc-5


【解决方案1】:

可以放置标签: 您还需要像这样指定一般错误代码和页面:

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Auto">
      <remove statusCode="403" subStatusCode="14"/>
      <error statusCode="403" subStatusCode="14" responseMode="ExecuteURL"
path="/App/Error/Forbidden"/>
      <remove statusCode="404"/>
      <error statusCode="404" responseMode="ExecuteURL" path="/App/Error/NotFound"/>
    </httpErrors>... 

记得在控制器错误中创建页面 NotFound!

【讨论】:

  • 这与 customErrors mode="On" 部分一起工作!
【解决方案2】:

我遇到了类似的问题,最终通过确保我执行了以下所有操作而得到解决:

1) 在 web.config 中设置 customErrors mode="On"

2) 创建了一个 ErrorController,其中包含针对需要自定义错误页面的特定错误的操作。

例如

    [AllowAnonymous]
    public virtual ActionResult NotFound(string message = "")
    {
        HttpContext.Response.StatusCode = 404;
        return View();
    }

3) 创建一个Filter.config文件来添加HandleErrorAttribute,并在global.asax的app start中调用这个方法。

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

4) 在 global.asax 中添加了全局错误处理程序。这是一个不完整的示例。

    void Application_Error(object sender, EventArgs e)
    {
        var exception = Server.GetLastError();
        Response.Clear();

        var httpException = exception as HttpException;

        if (httpException != null)
        {
            var action = string.Empty;

            switch (httpException.GetHttpCode())
            {
                case 401:
                    action = "Unauthorized";
                    break;
                case 403:
                    action = "Forbidden";
                    break;
                case 404:
                    action = "NotFound";
                    break;
                case 500:
                    action = "ServerError";
                    break;
                default:
                    action = "ServerError";
                    break;
            }

            Server.ClearError();

            Response.Redirect(string.Format("~/Error/{0}/?message={1}", action, exception.Message));
        }

        Logger.Error("Unhandled website exception", exception);
    }

所有这些都到位后,硬异常和故意重定向也将得到妥善处理。例如,在登录时未满足阻止用户继续操作的业务规则的情况下,已完成此操作。

return RedirectToAction("Unauthorized", "Error", new { message = "您的帐户被限制使用此功能。" });

不过,有一点建议是,返回 401 错误最终不会显示错误页面,而是会将您带到登录页面,除非您进行了适当的处理。我确定在其他帖子中对此进行了解释。

另一件事,我刚刚在将最新版本部署到我们的开发和 UAT 环境时发现了这一点。在我的本地 IIS 中,我看到自定义错误页面很好,但在其他环境中它显示的是默认 IIS 页面。路由到我的 ErrorController 工作得很好,但自定义页面仍然没有显示。原来我需要这个 web.config 条目

httpErrors existingResponse="PassThrough"

在 system.webServer 下,现在有意义了。

【讨论】:

    猜你喜欢
    • 2019-03-13
    • 2017-10-27
    • 2014-07-23
    • 2022-10-12
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多