Carlos Miguel Colanta
Carlos Miguel Colanta

Reputation: 2823

Throw an exception from an ajax request and catching it

i am trying to simulate exception management in ASP MVC,

Here is what i have:

public JsonResult callit()
{
        throw new HttpException(400, "Bad Request");
}

and then i try to call it like this:

$.ajax({
        type: 'GET',
        url: '@Url.Action("callit", "rat", new { area = "test" })',
        cache: 'false',
        error: function (request, status, error) {
            alert(request.responseText);
        }
});

But my application keeps on stopping when the exception is thrown and my error is not being triggered unless i stop the program. What should i do?

Upvotes: 1

Views: 871

Answers (1)

user3559349
user3559349

Reputation:

Rather than throwing an exception, return a HttpStatusCodeResult and change you method to ActionResult

public ActionResult callit() // or public HttpStatusCodeResult callit()
{  
    return new HttpStatusCodeResult(400, "Bad Request");
}

Upvotes: 3

Related Questions