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.JsonObjectAttributeますNewtonsoft.Json.JsonPropertyAttribute

于 2016-03-01T14:05:07.653 に答える
0

ついに見つけた!

web.api を扱うときは、モデル プロジェクトの標準 BOM プロデューサーに接続された「サービス プロデューサー」サブプロデューサーを追加することを絶対に忘れないでください。

属性が処理されず、非表示のプロパティ (オブジェクト フィールド) のみが出力時にシリアル化される場合、シリアル化時に問題に気付くことはありません。

オタクの間違いで申し訳ありませんが、学んだ教訓に満足しています。

改めまして、メジアントウ様、ありがとうございます。主に私がすべてのプロジェクトの詳細を質問に持ち込んでいないため、問題がどこで発生したかは決してわかりません.

于 2016-03-01T21:46:08.640 に答える