ASP.NET WebApi表单编码的字符串反序列化 [英] ASP.NET WebApi form-urlencoded string deserialize

查看:117
本文介绍了ASP.NET WebApi表单编码的字符串反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WebApi和Client开发服务,这些服务通过POST方法将压缩后的数据发送到该服务(以节省带宽).他们俩都在我的控制之下.在服务器上,我收到压缩数据,将其解压缩并具有以下字符串:

I develop service with WebApi and Client which send to this service gzipped data by POST method (to save bandwidth). Both of them over my control. On server I receive compressed data, decompress It and have string such as:

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字符串.

ie simple www-form-urlencoded string.

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

Does ASP.NET MVC4 WebApi have some method to bind or deserialize this string to my Model?

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

推荐答案

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

You may use the following code to parse the string:

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

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

The ReadAs is extension method defined under System.Web.Http.ModelBinding.FormDataCollectionExtensions.

这篇关于ASP.NET WebApi表单编码的字符串反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆