简体   繁体   中英

asp.net web api : bind request body values to the model class

In web API I have situation from client the request body is like below

{ "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.

Thanks Saroj

You can use the [JsonProperty(PropertyName="JSON_NAME"] in JSON.NET. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM