ASP.NET WebApi form-urlencoded string deserialize

血红的双手。 提交于 2020-04-16 10:43:31

问题


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

ie simple www-form-urlencoded string.

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; }
}

回答1:


You may use the following code to parse the string:

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

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



来源:https://stackoverflow.com/questions/12312055/asp-net-webapi-form-urlencoded-string-deserialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!