【问题标题】:MVC 6 XML API post won't bind to modelMVC 6 XML API 帖子不会绑定到模型
【发布时间】:2016-03-04 07:58:36
【问题描述】:

我的最终目标是在收到新 SMS 时接收 Twilio XML Posts,但现在我坚持将 XML 数据绑定到模型。

ASP.NET 5、MVC 6

我已启用 XML 格式化程序: services.AddMvc().AddXmlDataContractSerializerFormatters().AddXmlSerializerFormatters();

我有一个简单的模型类:

public class XmlTest
{
    public string PropertyOne { get; set; }
    public string PropertyTwo { get; set; }
}

还有一个简单的 API 方法:

[HttpPost]
public IActionResult Post(XmlTest xmlTest)
{
    //Application Logic
}

使用 PostMan,我发布了这个 XML 数据:

<?xml version="1.0" encoding="UTF-8"?>
<XmlTest>
   <PropertyOne>ValueOne</PropertyOne>
   <PropertyTwo>ghi789</PropertyTwo>
</XmlTest>

发布数据时,xmlTest 已初始化,但未设置 PropertyOne 和 PropertyTwo 的值。如果我将[FromBody] 属性添加到XmlTest,那么当post 发生时,xmlTest 的值为null。

谁能帮助我在这里做错了什么?

JSON 可以正常工作,但 Twilio 只会发布 xml。

谢谢!

【问题讨论】:

  • 您能告诉我们发布 XML 时使用的 HTTP 标头吗?

标签: asp.net xml http-post asp.net-core-mvc


【解决方案1】:

Twilio 布道者在这里。

为了确保我理解您的要求,您想在收到新消息时接收 Twilio 发送到消息请求 URL 的参数吗?

如果我理解正确,那么就没有必要处理 XML。 Twilio 发送的参数是表单编码值,因此您可以像将值从 HTML 表单发布到控制器一样处理它们:

public ActionResult Incoming(string Body, string From, string To) {

    //you can return TwiML back to Twilio here or nothing

}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多