【问题标题】:ASP.NET WebApi form-urlencoded string deserializeASP.NET WebApi form-urlencoded 字符串反序列化
【发布时间】:2012-09-07 05:09:31
【问题描述】:

我使用 WebApi 和客户端开发服务,通过 POST 方法向该服务发送压缩数据(以节省带宽)。他们两个都在我的控制之下。在服务器上我收到压缩数据,解压缩它并有字符串,例如:

section[0][caption]=Foo&
section[0][address]=175896&
section[0][counters][]=2&
section[0][counters][]=2&
section[0][errors][]=ERR_NOT_AVAILABLE&
errors=true&
trmtimestamp=1346931864358

即简单的 www-form-urlencoded 字符串。

ASP.NET MVC4 WebApi 是否有一些方法可以将此字符串绑定或反序列化到我的模型?

public class Snapshot
{
    public List<SectionState> sections { get; set; }
    public bool errors { get; set; }
    public double date { get; set; }
    public double trmtimestamp { get; set; }
}

【问题讨论】:

  • SectionState 是某种键/值对吗?
  • @Chris 不,这是简单的课程{ string caption, int address, int[] counters, string[] errors }

标签: asp.net-mvc-4 asp.net-web-api


【解决方案1】:

您可以使用以下代码来解析字符串:

new System.Net.Http.Formatting.FormDataCollection(query).ReadAs<Snapshot>();

ReadAs 是 System.Web.Http.ModelBinding.FormDataCollectionExtensions 下定义的扩展方法。

【讨论】:

  • 你是英雄孙红叶!
  • .NET Core 3.1 中有没有办法做到这一点? FormDataCollection 对象似乎丢失了。
猜你喜欢
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多