Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net web api 在webapi控制器中接受应用程序/xml内容_Asp.net Web Api_Asp.net Web Api2_Frombodyattribute - Fatal编程技术网

Asp.net web api 在webapi控制器中接受应用程序/xml内容

Asp.net web api 在webapi控制器中接受应用程序/xml内容,asp.net-web-api,asp.net-web-api2,frombodyattribute,Asp.net Web Api,Asp.net Web Api2,Frombodyattribute,我想创建一个接受application/xml内容的操作 基本上到目前为止我已经创建了这个 namespace App.Areas.Test.Controllers { public class UserModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; }

我想创建一个接受application/xml内容的操作

基本上到目前为止我已经创建了这个

namespace App.Areas.Test.Controllers
{

    public class UserModel
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }


    public class TestController : ApiController
    {
        [HttpPost]
        public UserModel Test([FromBody] UserModel um)
        {
            return um;
        }
    }
}
当我发布以下内容时

    <?xml version="1.0" encoding="UTF-8" ?>
    <UserModel>
    <FirstName>Some Name</FirstName>
    <LastName>Some Last Name</LastName>
    <Age>30</Age>
    </UserModel>

某个名字
一些姓
30
我最终得到了这样的回答

<UserModel i:nil="true" />


我尝试从body属性中删除
,但也没有效果。由于某些原因,内容未绑定到现有模型。

是否添加了
内容类型
应用程序\xml
标题?您需要通知序列化程序正文的格式。

这将实现此目的

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

XML文档具有多个根元素。可能需要将其包装在
标记中您是对的。我编辑了我的问题,但回答仍然是一样的