0

我有 mvc 3 应用程序,当在代码中抛出标准泛型 throw new Exception 时,会显示来自 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.");

      }
    }
4

2 回答 2

0

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

    try
    {
        foo.bar()      
    }
    catch(SpecificException)
    {
       RedirectToAction("500", "Error");
    }
于 2012-03-30T14:55:18.463 回答
0

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

于 2012-03-30T14:51:04.527 回答