JsonProperty似乎不能在windows azure应用程序服务上工作

本文关键字:应用程序 服务 工作 azure windows 不能 JsonProperty | 更新日期: 2023-09-27 18:05:57

我有一个托管在Microsoft Azure云上的应用程序。这个应用程序是一个应用服务。我目前在JSON序列化方面有一些问题。收到的JSON格式不正确。我的应用程序是一个ASP。净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或类似的东西…怎么了?非常感谢你的帮助!

JsonProperty似乎不能在windows azure应用程序服务上工作

在我看来,您的enum在本地模式下被序列化为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);
        }
    }
}

我关闭了这个帖子,因为从昨天开始,它就正常工作了。我什么也没做,只是摆弄了一下软件包的版本:——Microsoft.AspNet.Mvc——Newtonsoft。Json我想找出原因,但我缺乏线索。