c# - Xml Requests do not work -
i cannot bare bones asp.net core web api project work using xml instead of json. please help!
i have created new project , adjustments default configuration add xml formatters...
public void configureservices(iservicecollection services) { // add framework services. services.addapplicationinsightstelemetry(configuration); services.addmvc(config => { config.inputformatters.add(new xmlserializerinputformatter()); config.outputformatters.add(new xmlserializeroutputformatter()); }); }
my controller contains simple , post methods:
[route("api")] public class messagecontroller : controller { [httppost] public void post([frombody] message message) { } [httpget] public iactionresult get() { return ok(new message { testproperty = "test value" }); } }
when try calling post
method content-type: application/xml
, api returns 415 unsupported media type. have tried adding consumes("application/xml")
attribute controller , still not work.
the works , returns json. however, if add produces("application/xml")
attribute controller, returns 406 not acceptable, if provide accepts: application/xml
header.
for reason, api rejecting related xml though input , output formatters added have seen in few examples find.
what missing?
i have following thing in startup.cs , works xml , json both. here stick xml. note : ( have consider own class sample)
startup.cs
public void configureservices(iservicecollection services) { services.addmvccore() .addjsonformatters().addxmlserializerformatters(); }
my httpclient code ( might have missed content type setting have done in stringcotent)
two header important : accept , content-type. accept in content negotiation , content-type way client tell server type content client posting.
httpclient client = new httpclient(); client.baseaddress = new uri( @"http://localhost:5000"); client.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/xml")); httpcontent content = new stringcontent(@"<product> <id>122</id> <name>computer112</name></product>",system.text.encoding.utf8 , "application/xml"); // important. var result = client.postasync("/api/products", content).result;
Comments
Post a Comment