asp.net-mvc - ASP.Net MVC4 HandleError 未渲染指定 View

标签 asp.net-mvc razor exception

我有一个 Controller Action,它的 View 与应用程序的其余部分非常不同,并且我希望该 Action 抛出的任何异常都显示与应用程序的其余部分不同的错误页面。

我修改了 HandleError 属性,但当发生异常时它根本不加载错误页面。

    [HttpPost]
    [HandleError(View = "UPI_Error")]
    public ActionResult ParticipantUpdate1(Participant part)
    {
        try
        {
            mps.ParticipantUpdate(LogonTicket, ParticipantID, part);
        }
        catch(Exception ex)
        {
            string x = ex.Message;
        }
        return View();
    }

有什么想法吗?

更新

好的,修改为:

[HttpPost]
[HandleError(View = "UPI_Error", ExceptionType = typeof(ArgumentException))]
public ActionResult ParticipantUpdateSuccess(Participant part)
{
    // Copy the new stuff into the original ParticipantInfo before updating essentially "merging"
    Participant OrigPart = CopyParticipant(part);
    try
    {
        string result = mps.ParticipantUpdate(LogonTicket, ParticipantID, OrigPart);
        if (result != "Update Success")
        {
            throw new ArgumentException(result);
        }
    }
    catch (Exception e)
    {
        throw new ArgumentException(e.Message);
    }
    return View();
}

但是,它仍然不会加载 UPI_Error 页面。 ParticipantUpdate 调用抛出此异常:

System.ServiceModel.FaultException`1 was unhandled by user code
  HResult=-2146233087
  Message=ORA-12899: value too large for column "CLI"."CLI_MAIN_ZIP" (actual: 21, maximum: 11)

在此链接的文章中:http://www.c-sharpcorner.com/UploadFile/ff2f08/exception-or-error-handling-in-Asp-Net-mvc-using-handleerror/

它表示 HandleError 无法捕获 Controller 外部引发的异常。由于异常发生在我的 Web 服务中并传播到我的应用程序,这可能是问题的一部分吗?

但是当我从 Web 服务捕获异常时,我抛出了一个新的异常,并且它仍然显示 YSOD 与我的自定义错误页面。

更新

好吧,我决定走另一条路。我捕获异常,将异常对象复制到 TempData,然后调用我的自定义 View 。但是,由于某种原因,我无法更改 View 中的布局。它出现空引用异常。还有更多想法吗?

[HttpPost]
[HandleError(View = "UPI_Error")]
public ActionResult ParticipantUpdateSuccess(Participant part)
{
    bool error = false;
    // Copy the new stuff into the original ParticipantInfo before updating essentially "merging"
    Participant OrigPart = CopyParticipant(part);
    try
    {
        string result = mps.ParticipantUpdate(LogonTicket, ParticipantID, OrigPart);
        if (result != "Update Success")
        {
            throw new ArgumentException(result);
        }
    }
    catch (Exception ex)
    {
        error = true; //  throw new ArgumentException(e.Message);
        TempData["exception"] =  ex;
    }
    if (!error)
    {
        return View();
    }
    else
    {
        return RedirectToAction("UPIError"); 
    }
}

public ActionResult UPIError()
{
    ViewBag.Exception = TempData["exception"];
    return View();
}

抛出异常:

消息=未将对象引用设置为对象的实例。 堆栈跟踪: 在 ASP._Page_Views_Home_UPIError_cshtml.Execute() 中 ...\Views\Home\UPIError.cshtml:第 4 行

这是我的 View 中的第 4 行(是的,该 View 存在于共享文件夹中):

Layout = "~/Views/Shared/_UPILayout.cshtml";

遇到 View 命名问题,但已解决。现在,我仍然遇到空引用异常,但 View 在那里并且命名正确。

最佳答案

在错误处理程序中,您指定要对 ArgumentException 类型的异常使用自定义错误 View ,而当前引发的异常是 System.ServiceModel.FaultException 。您是否尝试过捕获更高的异常类,例如:

[HandleError(View = "UPI_Error", ExceptionType = typeof(Exception))]

或更具体地测试您的案例:

[HandleError(View = "UPI_Error", ExceptionType = typeof(System.ServiceModel.FaultException))]

关于asp.net-mvc - ASP.Net MVC4 HandleError 未渲染指定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34077050/

相关文章:

Java 错误 : New exception is thrown in catch block, 原始堆栈跟踪可能丢失

c# - 如何在不破坏我的服务层的情况下使用 MVCMailer?

mysql - 缺少模板中的 Visual Studio 2013 数据库

asp.net-mvc - 无法加载 Antlr3.Runtime,HRESULT : 0x80131040

c# - 为什么我的 ValidationMessageFor For TextArea 在页面加载时显示

java - mongoDB:java.lang.InknownClassChangeError:实现类

python - 如何最好地过滤其 __cause__ (或 __context__)的异常?

jquery - ASP MVC jquery 函数无法在部分 View 中工作,但在主视图中工作

javascript - 如何从 mvc View 调用 javascript 函数

匿名类型的 ASP.NET razor 三元表达式