Asp.net 为什么在MVC4中使用下划线序列化公共属性?
下课Asp.net 为什么在MVC4中使用下划线序列化公共属性?,asp.net,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,下课 using System; namespace Beverati.Repository.ViewModel { [Serializable] public class CollectionItem : EditableEntityBase { public CollectionItem() {} private int? _quantity; private string _retailValue; private string _currencyName; public int
using System;
namespace Beverati.Repository.ViewModel
{
[Serializable]
public class CollectionItem : EditableEntityBase
{
public CollectionItem() {}
private int? _quantity;
private string _retailValue;
private string _currencyName;
public int? quantity
{
get { return this._quantity ?? NULL_INTEGER; }
set { this._quantity = value; }
}
public string retailValue
{
get { return this._retailValue ?? String.Empty; }
set { this._retailValue = value; }
}
public string currencyName
{
get { return this._currencyName ?? String.Empty; }
set { this._currencyName = value; }
}
}
}
在此控制器操作中返回
public IEnumerable<Repository.ViewModel.CollectionItem> GetAll()
然而,当我安装MVC4时,返回的JSON如下所示
{
quantity:2
retailValue: 50.00
currencyName: USD
}
{
_quantity:2
_retailValue: 50.00
_currencyName: USD
}
所有属性名称都有下划线前缀。为什么要使用下划线,以及应该怎么做才能在JSON中返回公共属性名?就是[Serializable]属性在做什么-假设您不需要它用于其他用途-删除该属性,一切都应该正常。。有趣的是,在MVC2中,我需要该属性才能使序列化工作。您知道解释MVC4中[Serialize]属性效果的引用吗?