简体   繁体   English

ASP.NET MVC ModelBinder自定义字段映射

[英]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. 我有一个正在使用的api,它将对我的服务器端方法之一执行一些JSON POST。

I am working on creating some C# classes that map to that JSON's structure. 我正在创建一些映射到该JSON结构的C#类。 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.... 这是发送给我的JSON的示例。...

[
{
    "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? 我如何告诉模型绑定器采用json“对象”属性,并将其映射为C#类中的其他名称,因为对象是保留字?

 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. 如果要将c#保留关键字设置为变量/属性名称,请尝试在其前面加上“ @”前缀。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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