简体   繁体   English

asp.net Web API 2.1在控制器中使用内容类型应用程序xml获取空对象

[英]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个选项:

  1. Create a class for the parameter object instead of an anonymous type( object ). 为参数object而不是匿名type( object )创建一个类。
  2. Or if you want to receive the anonymous instance, you should remove XML formatter, because anonymous types are not supported by XML Formatter : 或者,如果您想接收匿名实例,则应删除XML格式化程序,因为XML格式化程序不支持匿名类型:

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.

相关问题 Not getting multiple select list json array in object parameter in api controller Asp.net Core 2.1 - Not getting multiple select list json array in object parameter in api controller Asp.net Core 2.1 ASP.NET MVC Web Api:服务器端的application / xml PUT请求主体为null - ASP.NET MVC Web Api: application/xml PUT request body is null on server side 在服务器端使用XML格式RestSharp的asp.net Web API操作上收到的空对象 - null object received on server side asp.net Web API action with XML format RestSharp Object getting null value FromForm in ASP.NET Core Web API - Object getting null value FromForm in ASP.NET Core Web API 如何在ASP.Net core 2.1 Web API中查看“默认控制器”页面? - How can I make a view of a Controller as Default page in ASP.Net core 2.1 Web API? POST字符串到ASP.NET Web Api应用程序 - 返回null - POST string to ASP.NET Web Api application - returns null 在ASP.NET Web API中更改响应内容类型 - Changing response content type in asp.net Web API ASP.NET Web API用户选择的内容类型 - asp.net web api user selected content type ASP.NET Core 2.1 Web.API更改应用程序见解的日志记录检测键 - ASP.NET Core 2.1 Web.API change application insights instrumentation key for logging 在正文中使用 int/string(简单类型)发布到 asp.net core web api 2.1 不起作用 - Post with int/string (simple type) in body to asp.net core web api 2.1 not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM