2016-07-11 2 views
0

나는 특정 날짜 형식을 원하고 내가MVC6 JsonConvert 세계적으로

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IHttpContextAccessor contextAccessor) 
    { 

..... 
    JsonConvert.DefaultSettings =() => new JsonSerializerSettings 
    { 
    DateFormatString = "dd/MM/yyy hh:mm:ss" ,     
    NullValueHandling = NullValueHandling.Ignore 

    }; 
..... 
를 null 값은 내가 Startup.cs 방법에 내 코드를 넣어 너무 무시하려는 구성 예를 들어 내 응용 프로그램의 특정 JSON 형식 요구 사항이

}

여기 내 액션 메소드

[HttpGet("[action]")] 
public ActionResult GetSampleyData() 
    { 
    var model= new {StartDate=DateTime.Now, Name="test"};    
    return new JsonResult(model); 
    } 

의 예입니다하지만 결과는 내가 예상하는 형식으로되지 않습니다. json 설정을 전역으로 설정하려면 어떻게해야합니까?

+1

asp.net-mvc6가 자체 내장'JsonSerializerSettings' 있습니다. 나는 당신이 [asp.net core 1.0 web api use camelcase] (https://stackoverflow.com/questions/38139607/asp-net-core-1-0-web-)에서 보여 지듯이 그것들에 접근하고 수정할 수 있어야한다고 생각한다. api-use-camelcase) – dbc

답변

1

Startup.cs 파일 ConfigureServices에서 services.AddMvc() 문을 찾아 다음 다음 코드를 추가합니다

 services.AddMvc() 
      .AddJsonOptions(o => 
      { 
       o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; 
       o.SerializerSettings.DateFormatString = "dd/MM/yyy hh:mm:ss"; 
      } 
     );