c# - Intercept webapi json formatting errors -
i'd have way intercept exception occurs when send in malformed json webapi endpoint, can homecoming semantic error code opposed 500. (e.g. "fix broken json or go hell")
you can create custom validation filter attribute deriving actionfilterattribute
:
public class validationfilterattribute : actionfilterattribute { public override void onactionexecuting(httpactioncontext actioncontext) { if (!actioncontext.modelstate.isvalid) { actioncontext.response = actioncontext .request .createerrorresponse(httpstatuscode.badrequest, actioncontext.modelstate); } } }
now, may either decorate actions it:
[httpget] [validationfilter()] public string dosomethingcool()
or register globally via config
:
config.filters.add(new validationfilterattribute());
c# .net json asp.net-web-api
No comments:
Post a Comment