ajax - Set default response type in WCF Web Api -
i've set of services hosted wcf web api , communicate them in json javascript. in cases i'm okay modifying accepts bit of header require json response there cases arising can't this. due the javascript framework i'm using (ext js). things lets me specify url , not proxy defaults such headers.
this isn't ext js question however. web api seems default returning xml, , i'd know whether it's possible change default can return json instead. in advance!
a bit of experimentation seems indicate order of configured formatters matter (which quite intuitive).
by default, when create instance of httpconfiguration
, formatters
collection contains these formatters:
- xmlmediatypeformatter
- jsonvaluemediatypeformatter
- jsonmediatypeformatter
- formurlencodedmediatypeformatter
the reason why xml default formatting because it's first formatter. make json default value, can reorder collection this:
- jsonvaluemediatypeformatter
- jsonmediatypeformatter
- xmlmediatypeformatter
- formurlencodedmediatypeformatter
given instance config
of httpconfiguration, here's 1 way reorder collection:
var jsonindex = math.max( config.formatters.indexof(config.formatters.jsonformatter), config.formatters.indexof(config.formatters.jsonvalueformatter)); var xmlindex = config.formatters.indexof( config.formatters.xmlformatter); config.formatters.insert(jsonindex + 1, config.formatters.xmlformatter); config.formatters.removeat(xmlindex);
whether or not supported don't know, seems work on webapi 0.6.0.
Comments
Post a Comment