ASP。创建一个一级JSON的WebAPI。

时间:2022-01-20 02:14:31

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. ASP。创建一个一级JSON的WebAPI。 When I set LazyLoadingEnabled to true then I'm getting JSON (on the left side in screen) with all related entities.

域汇编中有许多相关的实体。例如,具有家庭关系、房屋和人的导航属性(第1级)的人。除此之外,这些房子还有自己的资产净值。(第2层)至地址及地址(第3层)至城市、街道…当我将LazyLoadingEnabled设置为true时,我就会得到JSON(在屏幕的左边)和所有相关实体。

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?

如何只获得一个嵌套级别(如在scree的右侧)或将其他级别设置为NULL值(因为我设置了Newtonsoft.Json.NullValueHandling.Ignore)?我可以不使用.Include来实现它吗?

My class of People:

我们班的人:

    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 config:

WebAPI配置:

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

我没有任何解决办法,因为我没有足够的经验。所以,我需要你的建议和建议。对不起,我的英语不好,如果我需要补充更多的信息,请告诉我。谢谢

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.

更新我尝试添加[JsonIgnore]或忽略映射类中的属性,但当我请求get/House时,我需要从没有导航的地址获取字段。当请求获取/访问时,我不需要地址。结果我吃不下去了。

2 个解决方案

#1


1  

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

不要将跟踪的对象返回给控制器。您的业务逻辑代码(不应该存在于控制器中)应该将您的数据库感知对象映射到POCOs。这可以像使用一样简单

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

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

你在映射配置文件中设置忽略这些属性,这样它们就不会被复制。

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

注意,我的automap -fu对语法是粗略代码感到生疏。

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.

对于任何盲目映射,你都要非常小心,因为它会让你暴露出质量分配漏洞。这同样适用于直接访问活动跟踪对象。如果用户在其数据IsAdmin: false中看到,他们可能会得到狡猾的IsAdmin: true。这可以保存到您的数据库与盲分配。

#2


0  

If you are look for a way to ignore navigation properties in json serialization, this answer can help and you can ignore navigation properties in json serialization by it.

如果您正在寻找一种在json序列化中忽略导航属性的方法,这个答案可以帮助您,您可以忽略json序列化中的导航属性。

#1


1  

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

不要将跟踪的对象返回给控制器。您的业务逻辑代码(不应该存在于控制器中)应该将您的数据库感知对象映射到POCOs。这可以像使用一样简单

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

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

你在映射配置文件中设置忽略这些属性,这样它们就不会被复制。

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

注意,我的automap -fu对语法是粗略代码感到生疏。

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.

对于任何盲目映射,你都要非常小心,因为它会让你暴露出质量分配漏洞。这同样适用于直接访问活动跟踪对象。如果用户在其数据IsAdmin: false中看到,他们可能会得到狡猾的IsAdmin: true。这可以保存到您的数据库与盲分配。

#2


0  

If you are look for a way to ignore navigation properties in json serialization, this answer can help and you can ignore navigation properties in json serialization by it.

如果您正在寻找一种在json序列化中忽略导航属性的方法,这个答案可以帮助您,您可以忽略json序列化中的导航属性。