1

我正在经历一种奇怪的行为。我的 web.api 在 GET 请求中仅返回来自我的 ObjectCollection 的隐藏字段。这是我的控制器:

// GET: api/UserDocuments
[Route("api/UserDocuments/User/{userName}")]
public List<DocIndex> Get(string userName)
{
    User usuari = Humanisme.User.LoadByUserName(userName);
    List<DocIndex> resposta = DocumentCollection.LoadIndexPerUsuari(usuari);
    return resposta;
}

这是从 BOM 生成的对象:

    namespace Humanisme
    {
    using CodeFluent.Runtime;
    using CodeFluent.Runtime.Utilities;


    // CodeFluent Entities generated (http://www.softfluent.com). Date: Tuesday, 01 March 2016 11:52.
    // Build:1.0.61214.0820
    [System.CodeDom.Compiler.GeneratedCodeAttribute("CodeFluent Entities", "1.0.61214.0820")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DataObjectAttribute()]
    public partial class DocIndex : CodeFluent.Runtime.ICodeFluentLightEntity
    {

        private int _id = -1;

        [System.NonSerializedAttribute()]
        private Humanisme.User _user = ((Humanisme.User)(null));

        private string _lat = default(string);

        private string _lon = default(string);

        private string _etapaVital = default(string);

        private string _solvencia = default(string);

        private int _valoracio = CodeFluentPersistence.DefaultInt32Value;

        private System.DateTime _data = CodeFluentPersistence.DefaultDateTimeValue;

        private string _nom = default(string);

        public DocIndex()
        {
        }

        [System.ComponentModel.DefaultValueAttribute(((int)(-1)))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(int))]
        [System.ComponentModel.DataObjectFieldAttribute(true)]
        public int Id
        {
            get
            {
                return this._id;
            }
            set
            {
                this._id = value;
            }
        }

        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public Humanisme.User User
        {
            get
            {
                return this._user;
            }
            set
            {
                this._user = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Lat
        {
            get
            {
                return this._lat;
            }
            set
            {
                this._lat = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Lon
        {
            get
            {
                return this._lon;
            }
            set
            {
                this._lon = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string EtapaVital
        {
            get
            {
                return this._etapaVital;
            }
            set
            {
                this._etapaVital = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Solvencia
        {
            get
            {
                return this._solvencia;
            }
            set
            {
                this._solvencia = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(CodeFluentPersistence.DefaultInt32Value)]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(int))]
        public int Valoracio
        {
            get
            {
                return this._valoracio;
            }
            set
            {
                this._valoracio = value;
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(IsNullable=false, Type=typeof(System.DateTime))]
        public System.DateTime Data
        {
            get
            {
                return this._data;
            }
            set
            {
                this._data = value;
            }
        }

        [System.ComponentModel.DefaultValueAttribute(default(string))]
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Type=typeof(string))]
        public string Nom
        {
            get
            {
                return this._nom;
            }
            set
            {
                this._nom = value;
            }
        }

        protected virtual void ReadRecord(System.Data.IDataReader reader, CodeFluent.Runtime.CodeFluentReloadOptions options)
        {
            if ((reader == null))
            {
                throw new System.ArgumentNullException("reader");
            }
            if ((((options & CodeFluent.Runtime.CodeFluentReloadOptions.Properties) 
                        == 0) 
                        == false))
            {
                this._id = CodeFluentPersistence.GetReaderValue(reader, "Id", ((int)(-1)));
                this._user = new Humanisme.User();
                CodeFluent.Runtime.CodeFluentLightWeightPersistence.ReadRecord(reader, this._user, null, new CodeFluent.Runtime.Utilities.Pair<string, string>("Id", "User_Id"));
                this._lat = CodeFluentPersistence.GetReaderValue(reader, "Lat", ((string)(default(string))));
                this._lon = CodeFluentPersistence.GetReaderValue(reader, "Lon", ((string)(default(string))));
                this._etapaVital = CodeFluentPersistence.GetReaderValue(reader, "EtapaVital", ((string)(default(string))));
                this._solvencia = CodeFluentPersistence.GetReaderValue(reader, "Solvencia", ((string)(default(string))));
                this._valoracio = CodeFluentPersistence.GetReaderValue(reader, "Valoracio", ((int)(CodeFluentPersistence.DefaultInt32Value)));
                this._data = CodeFluentPersistence.GetReaderValue(reader, "Data", ((System.DateTime)(CodeFluentPersistence.DefaultDateTimeValue)));
                this._nom = CodeFluentPersistence.GetReaderValue(reader, "Nom", ((string)(default(string))));
            }
        }

        void CodeFluent.Runtime.ICodeFluentLightEntity.ReadRecord(System.Data.IDataReader reader)
        {
            this.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
        }
    }
}

调用 web.api get 方法会返回这个 JSON:

[
  {
    "_id": 1,
    "_lat": null,
    "_lon": null,
    "_etapaVital": null,
    "_solvencia": null,
    "_valoracio": 0,
    "_data": "0001-01-01T00:00:00",
    "_nom": null
  }
]

序列化器(来自 WebApiConfig.cs)

JsonMediaTypeFormatter jsonFormatter = (JsonMediaTypeFormatter)config.Formatters.FirstOrDefault(f => f is JsonMediaTypeFormatter);
        if (jsonFormatter != null)
        {
           // jsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include;
            jsonFormatter.UseDataContractJsonSerializer = true;
        }
4

2 回答 2

3

CodeFluent 实体生成的类由SerializableAttribute. 此属性更改Json.NET序列化或反序列化对象的方式。您可以配置Json.NET为忽略此属性:

JsonMediaTypeFormatter jsonFormatter = (JsonMediaTypeFormatter)config.Formatters.FirstOrDefault(f => f is JsonMediaTypeFormatter);
if (jsonFormatter != null)
{
    jsonFormatter.SerializerSettings.ContractResolver = new DefaultContractResolver()
    {
        IgnoreSerializableAttribute = true
    };
}

http://james.newtonking.com/archive/2012/04/11/json-net-4-5-release-2-serializable-support-and-bug-fixes

Json.NET 现在检测具有 SerializableAttribute 的类型并序列化该类型上的所有字段,包括公共的和私有的,并忽略属性

因此,您可以使用将添加 的服务生产者,DataMemberAttribute或者您可以使用Json.NET Aspect自动添加特定的 Json.NET 属性:Newtonsoft.Json.JsonObjectAttributeNewtonsoft.Json.JsonPropertyAttribute.

于 2016-03-01T14:05:07.653 回答
0

终于找到了!

在处理 web.api 时,永远,永远,永远不要忘记在模型项目中添加附加到标准 BOM 生产者的“服务生产者”子生产者。

您永远不会注意到任何问题,但在序列化时不会处理任何属性并且只会在输出时序列化隐藏的属性(对象字段)。

为书呆子的错误感到抱歉,为吸取的教训感到高兴。

再次感谢 Meziantou。您永远不会想到问题的根源,主要是因为我没有将所有项目细节带到问题中。

于 2016-03-01T21:46:08.640 回答