JsonProperty似乎不适用于Windows Azure应用服务
我有一个托管在Microsoft Azure云中的应用程序。此应用程序是一项应用程序服务 我目前有一些JSON序列化的问题。收到的JSON格式不正确。 我的应用程序是一个ASP.NET MVC6。 基本上,我有一个前台,他从控制器(服务器端)接收JSON值。JsonProperty似乎不适用于Windows Azure应用服务
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
或类似的东西...... 怎么了? 非常感谢您的帮助!
我关闭后,因为从昨天起,它的正常工作。 我什么也没做,除了玩包版: - Microsoft.AspNet.Mvc - Newtonsoft.Json 我想找到原因,但我缺乏线索。
在我看来,您的枚举在本地模式下序列化为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);
}
}
}
这些问题不仅在枚举上,而且在所有JSON属性名称上。 我想有 [ { “ID”: “806c76b5-GUID-GUID-GUID-GUID”, “SN”: “X00000000000” } ] 代替 [ { “Id”:“806c76b5-guid-guid-guid-guid”, “SerialNumber”:“X00000000000” } ] –
现在我明白了。我更新了答案...也许天青并没有使用Json.NET,所以你尝试重载默认的Json响应方法, – Nico
我试过了,但没有奏效。我仍然有一个糟糕的JSON .... –