【发布时间】:2016-07-11 15:54:08
【问题描述】:
我在我的应用程序中有特定的 Json 格式要求,例如我想要特定的日期格式并且我想要忽略空值,所以我将我的代码放在 Startup.cs 方法配置中
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
};
.....
}
这是我的操作方法的示例
[HttpGet("[action]")]
public ActionResult GetSampleyData()
{
var model= new {StartDate=DateTime.Now, Name="test"};
return new JsonResult(model);
}
但结果不是我预期的格式。如何全局设置 json 设置,以便我的所有操作方法都使用它。
【问题讨论】:
-
asp.net-mvc6 有自己的内部
JsonSerializerSettings。我认为您应该能够访问和修改它们,如asp.net core 1.0 web api use camelcase 所示