![](/img/trans.png)
[英]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.