ASP.NET MVC中的自定义页面错误500

时间:2015-02-10 15:25:18

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

我在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 shared文件夹中。

我不明白为什么没有采取行动Error500()

我安装了ELMAH。这可能是问题吗?

2 个答案:

答案 0 :(得分:0)

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

  

defaultRedirect指定用于将浏览器定向到的默认URL   发生错误。如果未指定defaultRedirect,则为一般错误   而是显示。 URL可以是绝对的(例如,   http://www.contoso.com/ErrorPage.htm)或者它可能是相对的。一个   相对URL(例如/ErrorPage.htm)与Web.config文件相关   指定defaultRedirect URL,而不是指定的网页   发生了错误。以波浪号(〜)开头的URL,例如   〜/ ErrorPage.htm,表示指定的URL相对于root   申请的路径。

答案 1 :(得分:0)

我也安装了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")';
                }
            }
        });