簡體   English   中英

使用JsonConvert.SerializeObject的序列化問題(服務器上缺少屬性)

[英]Serialization issue using JsonConvert.SerializeObject (properties missing on server)

使用JsonConvert.SerializeObject,我的行為越來越奇怪。

我有一個方法如下所示:

public class ServiceController : ApiController
{
    public object GetJSONConnectedResponse(object input)
    {
        return JsonConvert.SerializeObject(input, Formatting.Indented,
        new JsonSerializerSettings()
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Serialize
        });
    }


    public object GetMediaGallery(int? id)
    {
        try
        {
            return GetJSONConnectedResponse(GalleryBLL.GetMediaGallery(id));
        }
        catch (Exception exc)
        {
            LogBLL.LogException(exc, HttpContext.Current.Request.RawUrl);
            return null;
        }
    }
}

如您所見,我將MVC Web API用作服務應用程序,通過客戶端的javascript從中查詢ajax類型的數據。

方法GetMediaGallery返回具有以下結構的holder類:

public class MediaGalleryHolder
{
    public List<DB_Image> Images { get; set; }
    public List<string> SpinFrames { get; set; }
    public string FlyoverVideo { get; set; }
}

DB_Images是一個復雜的實體,我通過調用存儲過程以業務邏輯方法填充該實體。 在這個復雜的實體上,我添加了2個額外的屬性。

public partial class DB_Image
{
    public string FullPath { get; set; }
    public string ThumbPath { get; set; }
}

出於某種原因,在我的本地計算機上,結果正確地序列化了(同時添加了2個額外的屬性),但是在服務器上,那些額外的屬性並未添加到結果中(我知道它們已被填充,但是從未序列化)。

這是某種JSON.NET或MVC Web API版本控制問題,如何解決或解決此問題?

任何幫助將不勝感激!

最好,

部落84

您是否在每個課程上都嘗試了注釋? 您可以嘗試添加[可序列化]

  [Serializable] 
  public partial class DB_Image
  {
        public string FullPath { get; set; }
        public string ThumbPath { get; set; }
  }

  [Serializable]
  public class MediaGalleryHolder
  {
       public List<DB_Image> Images { get; set; }
       public List<string> SpinFrames { get; set; }
       public string FlyoverVideo { get; set; }
  }

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM