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:

  1. xmlmediatypeformatter
  2. jsonvaluemediatypeformatter
  3. jsonmediatypeformatter
  4. formurlencodedmediatypeformatter

the reason why xml default formatting because it's first formatter. make json default value, can reorder collection this:

  1. jsonvaluemediatypeformatter
  2. jsonmediatypeformatter
  3. xmlmediatypeformatter
  4. 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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -