简体   繁体   English

JsonProperty似乎无法在Windows Azure应用程序服务上运行

[英]JsonProperty seems to not working on a windows azure app service

I have an application hosted in Microsoft Azure cloud. 我有一个托管在Microsoft Azure云中的应用程序。 This application is an app service. 此应用程序是一个应用程序服务。 I currently have some issues with a JSON serialization. 我目前在JSON序列化方面遇到一些问题。 The received JSON is not on the correct format. 收到的JSON格式不正确。 My application is an ASP.NET MVC6. 我的应用程序是ASP.NET MVC6。 Basically, I have a front, who is receiving a JSON value from a controller (server side). 基本上,我有一个前台,他正在从控制器(服务器端)接收JSON值。

C# Server side method: C#服务器端方法:

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

Model class: 型号类别:

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
    .
    .
}

Correct JSON to receive: 更正JSON以接收:

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

When I am launching the application in local mode, everything is fine. 当我以本地模式启动应用程序时,一切都很好。 I have the correct JSON values and property names. 我有正确的JSON值和属性名称。 But on Azure, the front receive an incorrect JSON: 但是在Azure上,前端接收到错误的JSON:

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

I thought it was a difference of package versions between my local and those present on the Azure server, but everything looks fine. 我以为本地版本和Azure服务器版本之间的软件包版本有所不同,但是一切都很好。 The JsonProperty attribute seems to be ignored. JsonProperty属性似乎被忽略。 I am currently using Windows Azure SDK 2.8, Newtonsoft 9.0.1. 我当前正在使用Windows Azure SDK 2.8,Newtonsoft 9.0.1。 and AspNet.Mvc 6.0.0: 和AspNet.Mvc 6.0.0:

Dependencies: 依存关系:

"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"
  }

I don't know if I have some troubles with the JsonFormatter or something like that... What's wrong? 我不知道JsonFormatter或类似的东西有什么问题吗? Thanks a lot for your help! 非常感谢你的帮助!

It seems to me, that your enum is serialized as string in local mode and as int when deployed to Azure. 在我看来,您的枚举在本地模式下被序列化为string ,在部署到Azure时被序列化为int You could put a [JsonConverter(typeof(StringEnumConverter))] attribute over your enum to ensure, that it is serialized as as string . 您可以在枚举上放置[JsonConverter(typeof(StringEnumConverter))]属性,以确保将其序列化为string

If you want to string serialization per default, you could set the Json.NET default properties like so: 如果要默认使用string序列化,则可以像下面这样设置Json.NET默认属性:

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

To ensure that your controller does use Json.NET, you can overload the standard Json() method in the controller and use Json(myobject) instead of ObjectResult(myobject) : 为了确保您的控制器确实使用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);
        }
    }
}

I close the post, because since yesterday, it's working normally. 我关闭了该职位,因为从昨天开始,它一直在正常工作。 I did nothing excepts playing with the package version of: - Microsoft.AspNet.Mvc - Newtonsoft.Json I would like to find why, but I have a lack of clues. 除了使用以下软件包的版本外,我什么也没做:-Microsoft.AspNet.Mvc-Newtonsoft.Json我想找到原因,但是我缺乏线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MVVMLight:RelayCommand似乎无法在Windows 8 App中运行 - MVVMLight: RelayCommand seems not working in Windows 8 App Datetimeoffset 不适用于 azure 应用服务 - Datetimeoffset not working on azure app service Azure 应用服务似乎在 .NET 5 模式下启用了 NLS - Azure App Service seems to have NLS enabled in .NET 5 mode 从 .net windows 服务调用宁静的 api 服务似乎不起作用 - Calling a restful api from .net windows service seems to be not working COMPLUS_LoaderOptimization 在 Azure 应用服务中不起作用 - COMPLUS_LoaderOptimization not working in Azure app service SendGrid无法在Azure App Service中运行 - SendGrid not working from Azure App Service DataProtectionConfigurationProvider 在我的 Azure 应用服务上不起作用 - DataProtectionConfigurationProvider not working on my Azure app service Azure EventGridEvent 忽略 JsonProperty - Azure EventGridEvent ignores JsonProperty Windows Phone 8.1应用程序将文件上传到Azure移动应用程序服务 - Windows Phone 8.1 app upload a file to azure mobile app service Windows 8应用程序拒绝在运行代理的情况下连接到Azure服务 - Windows 8 app refuses to connect to Azure service with a proxy running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM