2016-09-07 4 views
0

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 값과 속성 이름을 가지고 있습니다. 그러나 푸른에 전면 잘못된 JSON을받을 수는 :

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

나는 내 로컬과 푸른 서버에 존재하는 그 사이의 패키지 버전의 차이라고 생각하지만, 모든 것이 잘 보인다. 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 또는 뭔가 약간의 문제가있는 경우 몰라요 ... 이 문제점은 무엇입니까? 도움을 주셔서 감사합니다.

답변

0

어제부터, 정상적으로 작동하고 있기 때문에 나는 포스트를 닫습니다. 패키지 버전으로 재생하는 것 외에는 아무 것도하지 않았습니다. - Microsoft.AspNet.Mvc - Newtonsoft.Json 이유를 찾고 싶지만 실마리가 부족합니다.

0

열거 형은 로컬 모드에서는 string으로, Azure로 배포 할 때는 int으로 직렬화됩니다. 열거 형에 [JsonConverter(typeof(StringEnumConverter))] 특성을 넣어서 string 같이 일련 번호를 지정할 수 있습니다. 기본적 당 string 직렬화하려면

, 그렇게 같은 Json.NET의 기본 속성을 설정할 수 있습니다 :

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

이 컨트롤러가 Json.NET을 사용 않도록하려면 표준 Json() 방법을 과부하 수 있습니다 컨트롤러에 ObjectResult(myobject) 대신 Json(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); 
     } 
    } 
} 
+0

문제는 enum뿐 아니라 모든 JSON 속성 이름에 있습니다. 나는 [{ "ID": "806c76b5이-GUID를-GUID를-GUID를-GUID" "SN"을 "X00000000000을" 을} ] 을하고 싶은 대신 을 [ { "Id": "806c76b5-guid-guid-guid-guid", "SerialNumber": "X00000000000" –

+0

이제 알겠습니다. 나는 대답을 업데이트했다. 아마도 azure는 Json.NET을 전혀 사용하지 않기 때문에 기본 Json 응답 메소드 인 – Nico

+0

을 과부하하려고 시도했지만 시도하지는 않았다. 나는 여전히 나쁜 JSON .... –

관련 문제