问题
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