【问题标题】:Custom page Error 500 in ASP.NET MVCASP.NET MVC 中的自定义页面错误 500
【发布时间】:2016-11-29 15:31:13
【问题描述】:

我在 web.config 中添加了以下内容:

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

还有我的 ErrorController:

public class ErrorController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Error404()
    {
        return View();
    }

    public ActionResult Error500()
    {
        return View();
    }

}

我有意见Error404Error500

错误 404 按预期工作,但错误 500 不工作。

动作Error500() 没有被调用,渲染的是一个名为Error.cshtml 的视图,它位于views 文件夹的de 共享文件夹中。

我不明白为什么不调用动作Error500()

我已经安装了 ELMAH。会不会是这个问题?

【问题讨论】:

  • 也许根本没有引发错误 500?从规范中:“500 内部服务器错误 - 服务器遇到了阻止它完成请求的意外情况。”
  • @Veverke 我在操作中添加了这行代码以引发错误:int u = Convert.ToInt32("");// Error line 我看到浏览器收到一个错误代码为 500 的页面
  • 我和你做了同样的事情,同样的事情发生了。还没有找到解决方案(我自己今天在网上搜索如何实现自定义错误页面)。

标签: asp.net-mvc-4 error-handling


【解决方案1】:

我也安装了 Elmah 并尝试了您的示例并得到相同的结果 - 错误 404 有效,但错误 500 无效。

可能是您在进行 Ajax 调用时收到错误 500 吗? 如果是这样,我通过添加这样的 statusCode 属性解决了这个问题。

 $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: '@Url.Action("GetCatalogue")',
            data: dropDownSelected,
            success: function (data) {
                location.href = '@Url.Action("GetPdfFile")/?fileNameGuid=' + encodeURIComponent(data); // download the actual file

            },
            statusCode: {
                500: function () {
                    kendo.ui.progress($("#loading"), false);
                    location.href = '@Url.Action("Error500", "Error")';
                }
            }
        });

【讨论】:

    【解决方案2】:

    这可能有效也可能无效,但请尝试将 defaultRedirect 元素设置为等于您的“~/Error/Error500”。

    defaultRedirect 指定将浏览器定向到的默认 URL,如果 发生错误。未指定 defaultRedirect 时,一般错误 而是显示。 URL 可能是绝对的(例如, http://www.contoso.com/ErrorPage.htm) 或者它可能是相对的。一个 /ErrorPage.htm 等相对 URL 是相对于 Web.config 文件的 指定默认重定向 URL,而不是指向其中的网页 发生错误。以波浪号 (~) 开头的 URL,例如 ~/ErrorPage.htm,表示指定的URL是相对于根的 应用程序的路径。

    【讨论】:

    • 这真的很奇怪。绝对应该调用 500 错误方法。即使没有默认重定向。
    猜你喜欢
    • 2011-01-23
    • 2018-12-03
    • 2015-03-08
    • 2014-06-27
    • 1970-01-01
    • 2015-11-14
    • 2011-08-13
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多