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

问题描述:

我加入我的web.config以下:自定义页面错误500在ASP.NET MVC

<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作品excpected但不是错误500

动作Error500()不叫,什么是呈现一种观点叫做Error.cshtml是里面去的意见文件夹的共享文件夹。

我不明白为什么行动Error500()没有被调用。

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

+0

也许错误500没有被提出?从规格:“500内部服务器错误 - 服务器遇到意外的情况,阻止它履行请求。” – Veverke 2015-02-10 15:26:41

+0

@Veverke我在我的行动中添加了这行代码,以引发一个错误:'int u = Convert.ToInt32 “”); //错误行'我看到浏览器接收到一个错误代码为500 – amp 2015-02-10 15:30:52

+0

的页面我和你一样,发生同样的情况。还没有找到一个解决方案(我自己在网上搜索如何实现自定义错误页面今天)。 – Veverke 2015-02-10 16:05:02

这可能会也可能不会,但可以尝试设置defaultRedirect元素等于您的“〜/ Error/Error500”。

defaultRedirect Specifies the default URL to direct a browser to if an error occurs. When defaultRedirect is not specified, a generic error is displayed instead. The URL may be absolute (for instance, http://www.contoso.com/ErrorPage.htm) or it may be relative. A relative URL such as /ErrorPage.htm is relative to the Web.config file that specified the defaultRedirect URL, not to the Web page in which the error occurred. A URL starting with a tilde (~),such as ~/ErrorPage.htm, means that the specified URL is relative to the root path of the application.

+0

不起作用... – amp 2015-02-10 17:49:45

+0

这真的很奇怪。应该肯定会调用500错误方法。即使没有默认的重定向。 – 2015-02-10 19:28:43

我也有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")'; 
       } 
      } 
     });