Web API returning JSON content type instead of XML -


i brand-spanking-new web api. working on proof of concept project team create ssrs 2012 reports based off of web-api xml data sources. however, web-api should not configured negotiate xml content type. in future phase, our web-applications should able retrieve json objects same controllers/actions.

i began following this tutorial , worked, no problem.

next configured routes can call actions directly, , added querystringmappings global.asax specify content type.

public class routeconfig {     public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { id = urlparameter.optional }         );     } }  public static class webapiconfig {     public static void register(httpconfiguration config)     {         config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{action}/{id}",             defaults: new { id = routeparameter.optional }         );     } }  public class webapiapplication : system.web.httpapplication {     protected void application_start()     {         arearegistration.registerallareas();          webapiconfig.register(globalconfiguration.configuration);         filterconfig.registerglobalfilters(globalfilters.filters);         routeconfig.registerroutes(routetable.routes);         bundleconfig.registerbundles(bundletable.bundles);          globalconfiguration.configuration.formatters.jsonformatter.addquerystringmapping("$format", "json", "application/json");         globalconfiguration.configuration.formatters.xmlformatter.addquerystringmapping("$format", "xml", "application/xml");     } } 

this works great , can use localhost:xxxxx/api/products/getallproducts/?$format=xml data source connection string in ssrs. i'm not sure if best way this, worked, stuck it. without query string mappings data source did not work in ssrs.

here ran trouble. branched off , created first model/controller/action. when run project in browser (chrome or ie 10) , try specify format xml (localhost:xxxxx/api/calcs/computefoocalculation?$format=xml), json result back. products controller continues work fine json or xml content types. reason action render json. here code looks like. let me know if need models or else. foocalculation has nested object bar. both foocalculation , bar have strings, doubles , datetimes.

controller:

public class calcscontroller : apicontroller {      public foocalculation computefoocalculation()     {         var foo = getfoo();         var bar = getbar();         var foocalculation = new foocalculation(foo, bar);          return foocalculation;     }  } 

sample json result:

{"foo":"xxx","foorate":{"foo":"xxx","bar":"sn","foobar":-1.00813e-05,"barfoo":-3.2644199999999995e-06,"fooobarrr":-4.17501e-06,"bardate":"2013-05-14t00:00:00"},"barrate":{"foo":"xxx","bar":"1w","foobar":-2.08687e-05,"barfoo":-3.11313e-05,"fooobarrr":-3.3e-05,"bardate":"2013-05-21t00:00:00"},"bardate":"2013-05-20t00:00:00","foodays":6,"foobar":-7.3741306716417904e-06,"bar":-0.0011149741306716415} 

thanks in advance help.

place parameterless constructor in model class. xml formatting trying instantiate empty xml tree prior retrieving of data.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -