MVC应用程序FaultException抛出如何显示错误页面

时间:2012-03-30 14:32:45

标签: asp.net-mvc wcf handle faultexception

我有mvc 3应用程序,当在代码中抛出标准泛型抛出新异常时,将显示来自Views \ Shared \ error.cshtml的错误页面。只需设置<customErrors mode="On"/>. (这是预期的和所需的)

即可完成此操作

应用程序在中间层使用WCF服务,当这些服务生成FaultException时,MVC没有显示错误页面,它显示了屏幕上用户的Web服务调用的详细信息。我想要做的就是处理代码中的错误并向用户显示Error.cshtml。我已经尝试过改变全球性的asax,但这种方法很有效。

   protected void Application_Error(object sender, EventArgs e)
    {
      Exception exception = Server.GetLastError();
      if (exception.GetType() == typeof(FaultException))
      {
        throw new Exception("There was a fault exception that i do not want to show details of to user.");

      }
    }

2 个答案:

答案 0 :(得分:0)

尝试创建一个ErrorController as asp.net MVC会尝试将您在web.config中指定的链接解析为{Controller} / {View},除非您指定它忽略该页面。此外,您可能希望应用属性来处理异常。

答案 1 :(得分:0)

您还可以创建错误控制器/视图,并在catch块中重定向到您选择的自定义错误页面

    try
    {
        foo.bar()      
    }
    catch(SpecificException)
    {
       RedirectToAction("500", "Error");
    }