对JSON输出在ASP.NET MVC应用程序过滤领域的最佳方式对象 [英] Best way to filter domain objects for json output in an ASP.NET MVC application
问题描述
如果我渲染asp.net常规视图MVC,在我的网页显示的那些我专门写出来的唯一的域对象的属性。例如:
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:
<div><%= Customer.FirstName %></div>
不过,如果我序列化域对象的JSON它将包括所有的财产。例如:
However, if I serialize a domain object for json it will include every property. Example:
public JsonResult Customer (int? id)
{
Customer customer = _serviceLayer.GetCustomer (id.Value);
return Json (customer);
}
因为我不想暴露了什么是在这种情况下筛选JSON输出性能的最佳方式每一位客户的财产?你可以像使用的UpdateModel一个包含/排除列表()?使用代理类,如公共类JsonCustomer?你有什么建议?
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?
推荐答案
我使用匿名类型是:
var customer = from c in serviceLayer.GetCustomers()
where c.Id == id.Value
select new { FirstName = c.FirstName };
这不仅是一个好主意。相反,它是抵御外主叫JSON()的时候,如果你的对象图包含循环引用,你会得到。
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输出在ASP.NET MVC应用程序过滤领域的最佳方式对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!