简体   繁体   中英

ASP.NET MVC ModelBinder Custom Field Mapping

I have a api that I am working with that is going to do a POST of some JSON to one of my server side methods.

I am working on creating some C# classes that map to that JSON's structure. My problem is one of the fields that is posted to me is named "object" and its a string.

Here is an example of the JSON that is sent to me....

[
{
    "subscription_id": "1",
    "object": "user",
    "object_id": "1234",
    "changed_aspect": "media",
    "time": 1297286541
},
{
    "subscription_id": "2",
    "object": "tag",
    "object_id": "nofilter",
    "changed_aspect": "media",
    "time": 1297286541
},]

Here is my issue. How do I tell the model binder to take the json "object" property and map it some a different name in my C# class since object is a reserved word?

 public class InstagramUpdate
{
    public string subscription_id { get; set; }
    public string object_id { get; set; }
    public string object { get; set; } //<-- what should I do here??
    public string changed_aspect { get; set; }
    public int time { get; set; }
}

Hope this makes sense?

Thanks!

just to have the answer here as well :

try to prefix your c# reserved keyword with "@" if you want to set it as a varible/attribute name.

ie:

public class InstagramUpdate
{
    public string subscription_id { get; set; }
    public string object_id { get; set; }
    public string @object { get; set; } //object will be here the prop name
    public string changed_aspect { get; set; }
    public int time { 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