I am developing a rest API on MVC3.
Whenever there is a problem with validation, I want to throw 500 + json that describes the error (the json can be the list of unvalidated fields).
The problem is that the json returns inside html that holds the entire HttpExeption (Server Error in '/' Application.)
If I put filterContext.ExceptionHandled = true; the message goes out clean, but the client can't see the 500 error on his side. This case: https://stackoverflow.com/a/4707762/936651 actually the html and gives clean json to the client, but also removes the 500 error.

有帮助吗?

解决方案

You could set the status code to 500 inside the custom error handler you have seen here:

filterContext.RequestContext.HttpContext.Response.StatusCode = 500;

and on the client:

$.ajax({
    url: '/home/foo',
    success: function (result) {
        alert('success');
    },
    error: function (jqXHR, textStatus, errorThrown) {
        var json = $.parseJSON(jqXHR.responseText);
        // TODO: do something with the json that was returned from the server
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top