在ASP中筛选json输出的域对象的最佳方法。净MVC应用程序

时间:2022-08-26 20:49:29

If I'm rendering a regular view in asp.net mvc the only domain object properties that show up in my page the ones I specifically write out. For example:

如果我在asp.net mvc中呈现一个常规视图那么在我的页面中出现的唯一域对象属性就是我特别写出来的。例如:

<div><%= Customer.FirstName %></div>

However, if I serialize a domain object for json it will include every property. Example:

但是,如果我为json序列化一个域对象,它将包含所有属性。例子:

public JsonResult Customer (int? id)
{
    Customer customer = _serviceLayer.GetCustomer (id.Value);

    return Json (customer);
}

Since I don't want every Customer property exposed what is the best way to filter the output properties for json in this case? Can you use an include/exclude list like UpdateModel()? Use a proxy class such as public class JsonCustomer? What would you recommend?

既然我不希望每个客户属性都公开,那么在这种情况下,过滤json输出属性的最佳方式是什么呢?可以使用包含/排除列表,比如UpdateModel()吗?使用代理类,比如public class JsonCustomer?你有什么建议?

6 个解决方案

#1


19  

I use anonymous types for this:

我使用匿名类型:

var customer = from c in serviceLayer.GetCustomers()
               where c.Id == id.Value
               select new { FirstName = c.FirstName };

This is not just a good idea. Rather, it's protection against the exception that you will get when calling Json() if your object graph contains a circular reference.

这不仅仅是一个好主意。相反,如果您的对象图包含循环引用,那么调用Json()时将会遇到异常,这是对这种异常的保护。

#2


6  

You may use the [ScriptIgnore] attribute (in System.Web.Extensions). See http://www.creave.dk/post/2009/10/07/Excluding-properties-from-being-serialized-in-ASPNET-MVC-JsonResult.aspx for an example.

您可以使用[ScriptIgnore]属性(在System.Web.Extensions)。参见http://www.creave.dk/post/2009/10/07/exclusive - property -being-serialized-in- aspnet - mvc - jsonresult.aspx示例。

#3


4  

Please use a view model. A view model is an object that the UI uses to represent your domain objects on the screen. Each screen has its own view model.

请使用视图模型。视图模型是UI用来在屏幕上表示域对象的对象。每个屏幕都有自己的视图模型。

When you make your view model, which is a DTO, which is a flattened, null-safe projection of domain objects, do not map properties you do not wish to be displayed on the screen.

当您创建视图模型时(它是一个DTO,是一个扁平的、空安全的域对象投影),不要映射不希望显示在屏幕上的属性。

Serialize the view model, not your domain object.

序列化视图模型,而不是域对象。

#4


1  

You could use the Newtonsoft library and [JsonIgnore] attribute for marking properties of your class you don't want to expose. There are other libraries (with possible different property ignore attribute name), I personally prefer this one since it's very flexible in JSON converter extensions etc + it can easily serialize anonymous objects.

您可以使用Newtonsoft库和[JsonIgnore]属性来标记您不想公开的类的属性。还有其他的库(可能有不同的属性忽略属性名),我个人更喜欢这个,因为它在JSON转换器扩展中非常灵活,它可以很容易地序列化匿名对象。

public class Customer
{
    ...
    [JsonIgnore]
    public string UrlIn { get; set; }
    public string FirstName { get; set; }
    // following example of a converter, you could write your own as well
    [JsonConverter(typeof(Newtonsoft.Json.Converters.JavaScriptDateTimeConverter))]
    public DateTime Created { get { return _created; } }
}

#5


0  

I ran into the same problem and THE ONE OF SOLUTION IS to

我遇到了同样的问题,解决方法之一是

Use [ScriptIgnore] atribute .. it will solve the problem.

使用[ScriptIgnore]卡拉季奇. .它将解决问题。

add system.web.extensions reference and add namespace:

添加包含。扩展引用和添加名称空间:

Using System.Web.Script.Serialization.

使用System.Web.Script.Serialization。

If you still have questions..Read on..for some detailed explanation..

如果你还有问题的话。继续阅读…对于一些详细解释. .

i have a User class with..

我有一个用户类。

using System;

使用系统;

using System.Collections.Generic;

使用System.Collections.Generic;

using System.Linq;

使用来;

using System.Web;

使用包含;

using Iesi.Collections.Generic;

使用Iesi.Collections.Generic;

using System.Runtime.Serialization;

使用System.Runtime.Serialization;

namespace RAPortal.Core.Entities

名称空间RAPortal.Core.Entities

{

{

public class User

公开课用户

{

{

   private int _userId;

   private string _firstName;

   private string _lastName;

private IList _applications;

私人IList _applications;

   private IList<Group> _groups;

   private IList<ApplicationRequest> _applicationRequests;

....Properties..

....属性. .

   public virtual int UserId

   {

       get

       {

           return _userId;

       }

       set

       {

           _userId = value;

       }

   }

   public virtual string Title

   {

       get

       {

           return _title;

       }

       set

       {

           _title = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

       }

   }

public virtual IList Groups

公共虚拟IList团体

   {

       get

       {

           return _groups;

       }

       set

       {

           _groups = value;

       }

   }

   public virtual IList<UserPrivilege> UserPrivileges

   {

       get

       {

           return _userPrivileges;

       }

       set

       {

           _userPrivileges = value;

       }

   }

   public virtual IList<UserRole> UserRoles

   {

       get

       {

           return _userRoles;

       }

       set

       {

           _userRoles = value;

       }

   }

...so on...

…等等……

and I have Groups class..

我有小组课。

using System;

使用系统;

using System.Collections.Generic;

使用System.Collections.Generic;

using System.Linq;

使用来;

using System.Web;

使用包含;

using System.Web.Script.Serialization;

使用System.Web.Script.Serialization;

using System.Runtime.Serialization;

使用System.Runtime.Serialization;

namespace RAPortal.Core.Entities

名称空间RAPortal.Core.Entities

{

{

public class Group

公开课组

{

{

   private int _groupId;

   private string _name;

   private IList<User> _users;

   public virtual int GroupId

   {

       get

       {

           return _groupId;

       }

       set

       {

           _groupId = value;

       }

   }

   public virtual string Name

   {

       get

       {

           return _name;

       }

       set

       {

           _name = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

       }

   }

   [ScriptIgnore]

   public virtual IList<User> Users

   {

       get

       {

           return _users;

       }

       set

       {

           _users = value;

       }

   }

}

}

}

}

Since User is referenced in the groups.. the json think that it is Circular reference and It will throw an exception..so the fix is to add [ScriptIgnore] on top of the User. And add the reference and namespace to this class like it..

因为用户在组中被引用。json认为是循环引用,会抛出异常。所以修复是在用户之上添加[ScriptIgnore]。并将引用和命名空间添加到这个类中。

It solved my problem .. I am sure there are better ways out there !!! Cheers...

它解决了我的问题。我相信有更好的办法!!欢呼声……

And remember you should add [scriptIgnore] only in the groups class and not in the Users class..

记住,你应该只在组类中添加[scriptIgnore],而不是在用户类中添加。

#6


0  

In my experience, the JavaScriptSerializer respects some of the XmlSerialization attributes such as XmlIgnore too.

根据我的经验,JavaScriptSerializer也尊重一些XmlSerialization属性,比如XmlIgnore。

#1


19  

I use anonymous types for this:

我使用匿名类型:

var customer = from c in serviceLayer.GetCustomers()
               where c.Id == id.Value
               select new { FirstName = c.FirstName };

This is not just a good idea. Rather, it's protection against the exception that you will get when calling Json() if your object graph contains a circular reference.

这不仅仅是一个好主意。相反,如果您的对象图包含循环引用,那么调用Json()时将会遇到异常,这是对这种异常的保护。

#2


6  

You may use the [ScriptIgnore] attribute (in System.Web.Extensions). See http://www.creave.dk/post/2009/10/07/Excluding-properties-from-being-serialized-in-ASPNET-MVC-JsonResult.aspx for an example.

您可以使用[ScriptIgnore]属性(在System.Web.Extensions)。参见http://www.creave.dk/post/2009/10/07/exclusive - property -being-serialized-in- aspnet - mvc - jsonresult.aspx示例。

#3


4  

Please use a view model. A view model is an object that the UI uses to represent your domain objects on the screen. Each screen has its own view model.

请使用视图模型。视图模型是UI用来在屏幕上表示域对象的对象。每个屏幕都有自己的视图模型。

When you make your view model, which is a DTO, which is a flattened, null-safe projection of domain objects, do not map properties you do not wish to be displayed on the screen.

当您创建视图模型时(它是一个DTO,是一个扁平的、空安全的域对象投影),不要映射不希望显示在屏幕上的属性。

Serialize the view model, not your domain object.

序列化视图模型,而不是域对象。

#4


1  

You could use the Newtonsoft library and [JsonIgnore] attribute for marking properties of your class you don't want to expose. There are other libraries (with possible different property ignore attribute name), I personally prefer this one since it's very flexible in JSON converter extensions etc + it can easily serialize anonymous objects.

您可以使用Newtonsoft库和[JsonIgnore]属性来标记您不想公开的类的属性。还有其他的库(可能有不同的属性忽略属性名),我个人更喜欢这个,因为它在JSON转换器扩展中非常灵活,它可以很容易地序列化匿名对象。

public class Customer
{
    ...
    [JsonIgnore]
    public string UrlIn { get; set; }
    public string FirstName { get; set; }
    // following example of a converter, you could write your own as well
    [JsonConverter(typeof(Newtonsoft.Json.Converters.JavaScriptDateTimeConverter))]
    public DateTime Created { get { return _created; } }
}

#5


0  

I ran into the same problem and THE ONE OF SOLUTION IS to

我遇到了同样的问题,解决方法之一是

Use [ScriptIgnore] atribute .. it will solve the problem.

使用[ScriptIgnore]卡拉季奇. .它将解决问题。

add system.web.extensions reference and add namespace:

添加包含。扩展引用和添加名称空间:

Using System.Web.Script.Serialization.

使用System.Web.Script.Serialization。

If you still have questions..Read on..for some detailed explanation..

如果你还有问题的话。继续阅读…对于一些详细解释. .

i have a User class with..

我有一个用户类。

using System;

使用系统;

using System.Collections.Generic;

使用System.Collections.Generic;

using System.Linq;

使用来;

using System.Web;

使用包含;

using Iesi.Collections.Generic;

使用Iesi.Collections.Generic;

using System.Runtime.Serialization;

使用System.Runtime.Serialization;

namespace RAPortal.Core.Entities

名称空间RAPortal.Core.Entities

{

{

public class User

公开课用户

{

{

   private int _userId;

   private string _firstName;

   private string _lastName;

private IList _applications;

私人IList _applications;

   private IList<Group> _groups;

   private IList<ApplicationRequest> _applicationRequests;

....Properties..

....属性. .

   public virtual int UserId

   {

       get

       {

           return _userId;

       }

       set

       {

           _userId = value;

       }

   }

   public virtual string Title

   {

       get

       {

           return _title;

       }

       set

       {

           _title = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

       }

   }

public virtual IList Groups

公共虚拟IList团体

   {

       get

       {

           return _groups;

       }

       set

       {

           _groups = value;

       }

   }

   public virtual IList<UserPrivilege> UserPrivileges

   {

       get

       {

           return _userPrivileges;

       }

       set

       {

           _userPrivileges = value;

       }

   }

   public virtual IList<UserRole> UserRoles

   {

       get

       {

           return _userRoles;

       }

       set

       {

           _userRoles = value;

       }

   }

...so on...

…等等……

and I have Groups class..

我有小组课。

using System;

使用系统;

using System.Collections.Generic;

使用System.Collections.Generic;

using System.Linq;

使用来;

using System.Web;

使用包含;

using System.Web.Script.Serialization;

使用System.Web.Script.Serialization;

using System.Runtime.Serialization;

使用System.Runtime.Serialization;

namespace RAPortal.Core.Entities

名称空间RAPortal.Core.Entities

{

{

public class Group

公开课组

{

{

   private int _groupId;

   private string _name;

   private IList<User> _users;

   public virtual int GroupId

   {

       get

       {

           return _groupId;

       }

       set

       {

           _groupId = value;

       }

   }

   public virtual string Name

   {

       get

       {

           return _name;

       }

       set

       {

           _name = !String.IsNullOrEmpty(value) ? value.ToUpper().Trim() : null;

       }

   }

   [ScriptIgnore]

   public virtual IList<User> Users

   {

       get

       {

           return _users;

       }

       set

       {

           _users = value;

       }

   }

}

}

}

}

Since User is referenced in the groups.. the json think that it is Circular reference and It will throw an exception..so the fix is to add [ScriptIgnore] on top of the User. And add the reference and namespace to this class like it..

因为用户在组中被引用。json认为是循环引用,会抛出异常。所以修复是在用户之上添加[ScriptIgnore]。并将引用和命名空间添加到这个类中。

It solved my problem .. I am sure there are better ways out there !!! Cheers...

它解决了我的问题。我相信有更好的办法!!欢呼声……

And remember you should add [scriptIgnore] only in the groups class and not in the Users class..

记住,你应该只在组类中添加[scriptIgnore],而不是在用户类中添加。

#6


0  

In my experience, the JavaScriptSerializer respects some of the XmlSerialization attributes such as XmlIgnore too.

根据我的经验,JavaScriptSerializer也尊重一些XmlSerialization属性,比如XmlIgnore。