ASP.NET的WebAPI创建一个级别JSON [英] ASP.NET WebAPI Creating one-level JSON

查看:187
本文介绍了ASP.NET的WebAPI创建一个级别JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有中域装配的许多相关实体。例如具有导航性能(1级),以 FamilyRelations 房屋。除了这个房屋都有自己的nav.prop(级别2),以地址地址(3级别)具有城市 ...等。

当我设置 LazyLoadingEnabled 与所有相关实体真的,那么我得到JSON(左侧屏幕)。

There are many related entities in Domain assembly. For example People that has navigation properties (Level1) to FamilyRelations, Houses and Persons. Beside this the Houses has own nav.prop (Level2) to Address and Address (Level3) has to City, Street ... etc. When I set LazyLoadingEnabled to true then I'm getting JSON (on the left side in screen) with all related entities.

我怎样才能得到只有一个级别嵌套(如右侧的卵石)或设置其他级别 NULL 值(因为我已经设置Newtonsoft.Json。 NullValueHandling.Ignore)?
结果
我可以实现它不使用 .INCLUDE 每个实体?

How can I get only one level of nesting (as on the right side in scree) or set other levels to NULL value (because I had setting Newtonsoft.Json.NullValueHandling.Ignore)?
Can I implement it without use .Include to each entity?

我级人物:

    public class People : BaseEntity
    {
        public int PersonID { get; set; }

        public int HouseID { get; set; }

        public int PeopleNumber { get; set; }

        public int? FamilyRelationID { get; set; }            

        //FK to House
        public virtual House Houses { get; set; }
        //FK to Person
        public virtual Person Persons { get; set; }
        //FK to FamilyRelations
        public virtual FamilyRelations FamilyRelations { get; set; }
    }

的WebAPI配置:

WebAPI config:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new   MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling
            = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling
             = Newtonsoft.Json.NullValueHandling.Ignore;

我没有任何解决办法,因为我没有用它足够的经验。结果所以,我需要你的建议,建议一下吧。对不起,我的英语水平,如果我不得不添加更多的信息,请让我知道。谢谢

I do not have any solution because I did not have enough experience with it.
So, I need your suggestions, advices about it. Sorry for my English and if I have to add more informations, please let me know. Thanks

更新结果
我试图映射类中添加 [JsonIgnore] 或忽略这些属性,但是当我做要求获取/众议院然后,我需要从获得场地址无nav.prop当要求获取/人然后我不NEDD 地址。因此,我不能ingnore它。

UPDATE
I've tried to add [JsonIgnore] or ignore those properties in mapping class but when I do request get/House then I need to get field from Address without nav.prop and when request get/People then I do not nedd Address. As a result I can't ingnore it.

推荐答案

从不跟踪对象返回到控制器。你的业务逻辑code(这不应该在控制器中存在)应映射数据库感知对象波苏斯。这可以是简单,只要用

Never return tracked objects to the controller. Your business logic code (which should not exist in the controller) should map your database aware objects to POCOs. This can be as simple as using

var poco = AutoMapper.Map<People>(livePerson)

和你设置你的映射轮廓忽视这些属性,所以他们不会被复制。

And you setup in your mapping profile to ignore those properties so they're not copied.

请注意我automapper福是生锈的语法是粗糙code。

Note my automapper-fu is rusty that syntax is rough code.

您要非常小心,任何盲目的映射,因为它可能让你到质量分配漏洞。这是直接将您的生活被跟踪的对象也是如此。如果用户看到他们的数据IsAdmin:假的,他们可能会狡猾和后IsAdmin:真。这可以保存到盲分配您的数据库。

You want to be very careful with any blind mapping as it opens you up to the Mass Assignment vulnerability. This is equally true for going straight to your live tracked objects. If a user sees in their data IsAdmin: false, they might get crafty and post IsAdmin: true. This can be saved to your database with blind assignments.

这篇关于ASP.NET的WebAPI创建一个级别JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆