[英]asp.net web api 2.1 getting null object in controller with content type application xml
I have Api controller which has only POST and PUT 我有只有POST和PUT的Api控制器
public HttpResponseMessage Post([FromBody]object msg)
{
_logger.Log.Info("Got message: " + msg.ToString());
return new HttpResponseMessage(HttpStatusCode.OK);
}
public HttpResponseMessage Put(object msg)
{
_logger.Log.Info("Got message: " + msg.ToString());
return new HttpResponseMessage(HttpStatusCode.OK);
}
3rd party component is sending me requests with content type of application/xml 第三方组件向我发送内容类型为application / xml的请求
<EventList>
<Event>
<EventSubTypeId>65</EventSubTypeId>
<ExternalEventId>1</ExternalEventId>
<Direction>West</Direction>
<Road>I 70</Road>
<StartMileMarker>71.0</StartMileMarker>
<EndMileMarker>72.0</EndMileMarker>
<IsBothDirectionFlag>false</IsBothDirectionFlag>
<StartDate>2014-09-08T15:01:01.190-06:00</StartDate>
<Latitude>39.941208</Latitude>
<Longitude>-105.140121</Longitude>
<FatalityFlag>false</FatalityFlag>
<HazmatFlag>false</HazmatFlag>
<EstimatedTimeToClear>Test Estimated time to clear</EstimatedTimeToClear>
<GroupName>ITS</GroupName>
<Severity>Minimal</Severity>
<Classification>None</Classification>
<RoadwayClosure>No Lanes Closed</RoadwayClosure>
</Event>
however no matter how I try I am always getting null in the msg object I have also tried 但是,无论我如何尝试,我在味精对象中总是得到null
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
what is the solution? 解决办法是什么?
public HttpResponseMessage Post([FromBody]string msg)
{
XDocument xmlMessage = XDocument.Parse(msg);
_logger.Log.Info("Got message: " + msg.ToString());
return new HttpResponseMessage(HttpStatusCode.OK);
}
There are 2 options: 有2个选项:
object
instead of an anonymous type( object
). 为参数object
而不是匿名type( object
)创建一个类。 The XML serializer does not support anonymous types or JObject instances. XML序列化器不支持匿名类型或JObject实例。 I 一世
Therefore in your code object msg
is not valid with XML Serializer. 因此,在您的代码object msg
对于XML序列化器无效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.