ASP.Net MVC4 HandleError not shown view view

I have one Action controller that has a completely different view than the rest of my application, and I want all exceptions thrown by this action to display a different error page than the rest of my application.

I changed the HandleError attribute, but it does NOT load the error page at all when an exception occurs.

    [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();
    }

Any ideas?

UPDATE

OK, changed to this:

[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();
}

But this STILL does not load the UPI_Error page. Calling "Member" returns this exception:

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)

The article follows this link: http://www.c-sharpcorner.com/UploadFile/ff2f08/exception-or-error-handling-in-Asp-Net-mvc-using-handleerror/

, HandleError , . - , ?

, -, - YSOD .

UPDATE

, . , TempData, . - . NULL-. ?

[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

4 (, ):

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

, . Null Reference, .

+4
4

, ArgumentException, System.ServiceModel.FaultException. Exception, :

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

, , :

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

, web.config. <system.web> :

<customErrors mode="On" defaultRedirect="Error"/>

"" .

( HomeController) . id, 0, ArgumentException , , ContactError. , 1, Error. 1,

[HandleError(View = "ContactError", ExceptionType = typeof(ArgumentException))]
public ActionResult Contact(int id)
{
    if (id == 0)
    {
        throw new ArgumentException();
    }
    if (id == 1)
    {
        throw new NotImplementedException();
    }

    ViewBag.Message = "Your contact page.";
    return View();
}
-1

, .

customErrors web.config

<system.web>
  <customErrors mode="On" />
</system.web>

ation HandleError .

[HandleError(View="MyError")]
public ActionResult ErrorTest()
{       
    throw new ArgumentException("Just-Throw-An-Exception");
    return View();
}

MyError.cshtml , ~/Views/{YourCurrentControllerName} ~/Views/Shared/.

-1

You catch an exception, but don't throw a new one in catch clouse. You must not catch an exception, or you must throw an exception so that the application page displays an error.

If you catch an exception in catch try catch, block your countinues code from the catch scope and make no mistakes.

You need the error that you selected and did not catch on the try catch block ro get error page.

-1
source

Source: https://habr.com/ru/post/1618496/


All Articles