[英]MVVMLight: RelayCommand seems not working in Windows 8 App
[英]JsonProperty seems to not working on a windows azure app service
我有一个托管在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
或类似的东西有什么问题吗? 非常感谢你的帮助!
在我看来,您的枚举在本地模式下被序列化为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我想找到原因,但是我缺乏线索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.