JsonProperty似乎没有在Windows Azure应用服务上工作

时间:2016-09-07 10:25:42

标签: c# json azure json.net asp.net-core-mvc

我有一个托管在Microsoft Azure云中的应用程序。此应用程序是一项应用服务。 我目前在JSON序列化方面存在一些问题。收到的JSON格式不正确。 我的应用程序是ASP.NET MVC6。 基本上,我有一个前端,从控制器(服务器端)接收JSON值。

C#服务器端方法:

[HttpGet]
[Route("domain/search")]
public IActionResult Search(string sn)
{
    // DOING MY STUFF
    ICollection<MyModel> myobject
    return new ObjectResult(myobject);
}

模特课程:

public class MyModel
{
    [JsonProperty(PropertyName = "id", NullValueHandling = NullValueHandling.Ignore)]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "sn", NullValueHandling = NullValueHandling.Ignore)]
    public string SerialNumber { get; set; }

    [JsonProperty(PropertyName = "state", NullValueHandling = NullValueHandling.Ignore)]
    public AStateTypeModel? State { get; set; }

    [JsonProperty(PropertyName = "description", NullValueHandling = NullValueHandling.Ignore)]
    public string Description { get; set; }

    .
    .
    ETC
    .
    .
}

更正要接收的JSON:

[  
   {  
      "id":"806c76b5-guid-guid-guid-guid",
      "sn":"X00000000000",
      "state":"AState",
      .
      .
      ETC
      .
      .
   }
]

当我以本地模式启动应用程序时,一切都很好。我有正确的JSON值和属性名称。 但是在Azure上,前端会收到不正确的JSON:

[  
   {  
      "Id":"806c76b5-guid-guid-guid-guid",
      "SerialNumber":"X00000000000",
      "Description":null,
      "State":1,
      .
      .
      ETC
      .
      .
   }
]

我认为我的本地和Azure服务器上的软件包版本不同,但一切看起来都很好。 JsonProperty属性似乎被忽略了。 我目前正在使用Windows Azure SDK 2.8,Newtonsoft 9.0.1。和AspNet.Mvc 6.0.0:

依赖关系:

"dependencies": {
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-*",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-*",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-*",
    "Microsoft.AspNet.StaticFiles": "1.0.0-*",
    "Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",
    "Microsoft.AspNet.Diagnostics": "1.0.0-*",
    "Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final",
    "Microsoft.AspNet.Owin": "1.0.0-rc1-final",
    "Microsoft.AspNet.SignalR.Redis": "2.2.0",
    "Microsoft.AspNet.WebSockets.Server": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.Cookies": "1.0.0-rc1-final",
    "Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-rc1-final",
    "Newtonsoft.Json": "9.0.1"
  }

我不知道我是否有JsonFormatter或类似的问题...... 怎么了? 非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

在我看来,您的枚举在本地模式下被序列化为string,在部署到Azure时被序列化为int。您可以在枚举上添加[JsonConverter(typeof(StringEnumConverter))]属性,以确保将其序列化为string

如果你想默认string序列化,你可以像这样设置Json.NET默认属性:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    Converters =
    {
        new StringEnumConverter
        {
            CamelCaseText = true
        }
    }
};

为了确保您的控制器确实使用Json.NET,您可以重载控制器中的标准Json()方法并使用Json(myobject)而不是ObjectResult(myobject)

protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonNetResult(base.Json(data, contentType, contentEncoding, behavior));
}

private class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        this.ContentType = "application/json";
    }

    public JsonNetResult(JsonResult existing)
    {
        this.ContentEncoding = existing.ContentEncoding;
        this.ContentType = !string.IsNullOrWhiteSpace(existing.ContentType) ? existing.ContentType : "application/json";
        this.Data = existing.Data;
        this.JsonRequestBehavior = existing.JsonRequestBehavior;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }
        if ((this.JsonRequestBehavior == JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
        {
            base.ExecuteResult(context);                            // Delegate back to allow the default exception to be thrown
        }

        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = this.ContentType;

        if (this.ContentEncoding != null)
        {
            response.ContentEncoding = this.ContentEncoding;
        }

        if (this.Data != null)
        {
            // Replace with your favourite serializer.
            new Newtonsoft.Json.JsonSerializer().Serialize(response.Output, this.Data);
        }
    }
}

答案 1 :(得分:0)

我关闭帖子,因为从昨天开始,它正常工作。 我没有做任何事情除了玩包的版本: - Microsoft.AspNet.Mvc - Newtonsoft.Json 我想找到原因,但我缺乏线索。