asp.net web api:将请求体值绑定到模型类

时间:2017-01-25 07:13:35

标签: c# asp.net-web-api

在Web API中我有来自客户端的情况请求正文如下

{ "device-id":100101,"device-name": "Samsung 001"}


my model class
public class DeviceDesc
{
    public string device_id {get;set;}
    public string device_name {get;set;}
}

So the values are not binding to the properties.  In c# we cannot declare         properties with " - " symbol.  So how I can do this in web api.  the request body cannot be changed bcoz the JSON is generated by some third-party app.  Please, any solution for this.

由于 Saroj

1 个答案:

答案 0 :(得分:1)

您可以在JSON.NET中使用[JsonProperty(PropertyName="JSON_NAME"]。例如:

public class DeviceDesc
{
    [JsonProperty(PropertyName="device-id"]
    public string device_id {get;set;}
    [JsonProperty(PropertyName="device-name"]
    public string device_name {get;set;}
}