【问题标题】:Error Pages Are Not Being Routed To From Web.config错误页面未从 Web.config 路由到
【发布时间】:2018-04-06 17:20:18
【问题描述】:

我终于能够让 404 和 500 错误重定向,但是当他们这样做时,它们给了我两个结果之一。

当我使用它时(特别是 404):

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <remove statusCode="500"/>
  <error statusCode="404" path="/Home/PageNotFound" responseMode="ExecuteURL" />
  <error statusCode="500" path="/Home/InternalServerError" responseMode="ExecuteURL" />
</httpErrors>

然后输入一个不存在的链接(例如,localhost:11111/yo),我得到一个没有任何内容的白页。

当我使用它时(特别是 404s):

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <remove statusCode="500"/>
  <error statusCode="404" path="/Home/PageNotFound" responseMode="File" />
  <error statusCode="500" path="/Home/InternalServerError" responseMode="File" />
</httpErrors>

然后输入一个不存在的链接(例如,localhost:11111/yo),我会在白页上看到这行文本:

您要查找的资源已被删除、名称已更改或暂时不可用。

我在两个错误页面中都设置了断点,但它们在两个示例中都没有命中

这是我的错误页面:

public ActionResult PageNotFound()
{
    Response.StatusCode = 404;
    return View();
}
public ActionResult InternalServerError()
{
    Response.StatusCode = 500;
    return View();
}

它们在 HomeController 内部,因此是 path="/Home/...

如何做到这一点,以便在发生错误时触发(我假设 404 的解决方案与 500 的解决方案相同)。我在我的 404 和 500 页面中放了一个段落,上面写着“嗨”。这就是我知道它会起作用的方式。

我也用斜线尝试了这个解决方案,但它不起作用web.config errors fail with responseMode="File"

【问题讨论】:

    标签: c# asp.net-mvc custom-error-pages


    【解决方案1】:

    Web.config 设置

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

    错误控制器

    public class ErrorController : Controller
    {
        public ViewResult Index()
        {
            return View("Error");
        }
        public ViewResult NotFound()
        {
            Response.StatusCode = 404;  //you may want to set this to 200
            return View("NotFound");
        }
    }
    

    Error.cshtml 页面

     @model System.Web.Mvc.HandleErrorInfo
    @{
        Layout = "_Layout.cshtml";
        ViewBag.Title = "Error";
    }
    <div class="list-header clearfix">
        <span>Error</span>
    </div>
    <div class="list-sfs-holder">
        <div class="alert alert-error">
            An unexpected error has occurred. Please contact the system administrator.
        </div>
        @if (Model != null && HttpContext.Current.IsDebuggingEnabled)
        {
            <div>
                <p>
                    <b>Exception:</b> @Model.Exception.Message<br />
                    <b>Controller:</b> @Model.ControllerName<br />
                    <b>Action:</b> @Model.ActionName
                </p>
                <div style="overflow:scroll">
                    <pre>
                        @Model.Exception.StackTrace
                    </pre>
                </div>
            </div>
        }
    </div>
    

    【讨论】:

    • @BradleyWilliamElko 不,您可以使用另一个控制器名称
    • @BradleyWilliamElko 在 IIS 中托管后执行此设置
    • @BradleyWilliamElko in 在 Visual Studio 中不起作用,如果您在 IIS 服务器上托管,那么它将起作用。
    猜你喜欢
    • 2019-03-20
    • 2017-06-17
    • 1970-01-01
    • 2014-11-06
    • 2018-06-26
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多