c# - WebApi2 OData return custom error object -
i've searched intensive can't work.
i've haven web ap2 odata api , need return custom error class.
here have:
public class error { public string code { get; set; } public string message { get; set; } }
in global.asax.cs
protected void application_start() { globalconfiguration.configure(webapiconfig.register); globalconfiguration.configuration.messagehandlers.add(new messageinterceptor()); } public class messageinterceptor : delegatinghandler { protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) { return base.sendasync(request, cancellationtoken).continuewith( task => { var body = task.result.content.readasstringasync().result; var resultobj = jsonconvert.deserializeobject(body.replace("value", "results")); task.result.content = new objectcontent(typeof(object), resultobj, new jsonmediatypeformatter()); return task.result; }, cancellationtoken); } }
in webapiconfig,cs
public static class webapiconfig { public static void register(httpconfiguration config) { config.filters.add(new handleapiexceptionattribute()); } } public class handleapiexceptionattribute : exceptionfilterattribute, iexceptionfilter { public override void onexception(httpactionexecutedcontext context) { if (context.exception error) { var res = context.exception.message; //define response message httpresponsemessage response = new httpresponsemessage(httpstatuscode.internalservererror) { content = new stringcontent(res), reasonphrase = res }; //create error response context.response = response; } } public class orderscontroller : odatacontrollera controller { private context db = new context(); // get: odata/orders [queryable] public iqueryable<orders> getorders(odataqueryoptions<orders> opts) { <some code producing error> error theerror = new error() { code = "1000", message = "geen filter gespecificeerd" }; httpresponsemessage response = request.createresponse(httpstatuscode.badrequest, theerror); //return request.createresponse(httpstatuscode.badrequest, theerror); throw new httpresponseexception(response);
}
when try this crashes in messageinterceptor.
this there because third party consuming api want's in specific format.
when code runs correct returns results{}
on error should return error{code:, message: }
anyone ideas?
jeroen
this came with:
i changed controller to:
[queryable, basicauthentication] public ihttpactionresult getorders(odataqueryoptions<orders> opts) { else { error theerror = new error() { code = "1000", message = "geen filter gespecificeerd" }; return new errorresult(theerror, request); } return ok(resultlist); }
using class:
public class errorresult : ihttpactionresult { error _error; httprequestmessage _request; public errorresult(error error, httprequestmessage request) { _error = error; _request = request; } public task<httpresponsemessage> executeasync(cancellationtoken cancellationtoken) { list<error> _errorlist = new list<error>(); _errorlist.add(_error); error err = new error() { errors = _errorlist }; var response = new httpresponsemessage(httpstatuscode.badrequest) { content = new objectcontent<error>(err, new jsonmediatypeformatter()), requestmessage = _request }; return task.fromresult(response); } } public class error { public string code { get; set; } public string message { get; set; } } public class error { public list<error> errors { get; set; } }
Comments
Post a Comment