Friday, 15 July 2011

c# - HttpResponseMessage content is not getting serialized for BadRequest responses -



c# - HttpResponseMessage content is not getting serialized for BadRequest responses -

i have asp.net application, mvc , webapi controllers in there. mvc 5.1 , webapi 2.1. there no tricky configuration @ all, thing getting configured removed xml formatter:

public static class webapi { public static void configure(httpconfiguration config) { var formatters = config.formatters; formatters.remove(formatters.xmlformatter); } }

there simple dto set info returned controller action together:

public class tokenresponse { public string token { get; set; } public string token_type { get; set; } public int expires_in { get; set; } public string error { get; set; } }

and next code:

[nohttpresponsecaching] public class tokencontroller : apicontroller { public async task<httpresponsemessage> postasync(httprequestmessage request, tokenrequest tokenrequest) { seek { // stuff var response = new tokenresponse { ... }; homecoming request.createresponse(httpstatuscode.ok, response); } grab (tokenrequestvalidationexception ex) { _logger.writeerror(ex); var response = new tokenresponse { error = ex.errortodisplay }; homecoming request.createresponse(httpstatuscode.badrequest, response); } } }

the problem if fine, have tokenresponse serialized json, expected, if there exception occurring, execution flow coming grab block , response body equal "bad request", raw dump of response fiddler:

http/1.1 400 bad request cache-control: no-store pragma: no-cache content-type: text/html server: microsoft-iis/8.5 x-powered-by: asp.net date: wed, 18 jun 2014 08:25:53 gmt content-length: 11 bad request

tried homecoming anonymous object having random named properties instead of using tokenresponse, getting same response:

return request.createresponse(httpstatuscode.badrequest, new {klhjaoiubf = "kjhaflkjh"});

and i'm stuck @ point trying understand why i'm not getting object serialized in response body , how alter it. anyone, ideas, why object getting ignored when response code badrequest?

c# asp.net .net asp.net-web-api asp.net-web-api2

No comments:

Post a Comment