when asp.net mvc throws exception, returns 500 error response type text/html
- which, of course, invalid json.
i want respond ajax request expecting json error can receive , display user.
is possible return json http status code of 500?
when problem missing parameter, 500 error occurs before controller called - controller solution might not work. example leaving required parameter out in call action returns jsonresult, asp.net mvc sends client:
server error in '/' application. parameters dictionary contains null entry parameter 'id' of non-nullable type 'system.int32' method 'system.web.mvc.jsonresult edituser(int32, system.string, system.string, system.string, system.string, system.string, system.string, system.string, system.string)' in 'bhh'. optional parameter must reference type, nullable type, or declared optional parameter. parameter name: parameters
i'm using jquery; there better way handle this?
you use custom error handler filter:
public class ajaxerrorhandler : filterattribute, iexceptionfilter { public void onexception(exceptioncontext filtercontext) { if (filtercontext.httpcontext.request.isajaxrequest()) { filtercontext.exceptionhandled = true; filtercontext.result = new jsonresult { data = new { errormessage = "some error message" } }; } } }
and decorate controller/actions calling through ajax or register global filter.
then when performing ajax request can test presence of error property:
$.getjson('/foo', function(result) { if (result.errormessage) { // went wrong on server } else { // process } });
Comments
Post a Comment