ISO UTC DateTime格式作为MVC 6 API响应中的默认json输出格式 [英] ISO UTC DateTime format as default json output format in MVC 6 API response

查看:101
本文介绍了ISO UTC DateTime格式作为MVC 6 API响应中的默认json输出格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道返回DateTime对象时如何将MVC6的json输出配置为默认的ISO UTC DateTime字符串格式吗?

Does anyone know how to configure MVC6's json output to default to a ISO UTC DateTime string format when returning DateTime objects?

在WebApi2中,我可以设置JsonFormatter SerializerSettings并转换日期时间,但是我对如何在MVC6中做到这一点有些困惑

In WebApi2 I could set the JsonFormatter SerializerSettings and convert datetimes however i'm a bit stuck with how to do this in MVC6

推荐答案

我偶然发现了可以帮助我解决问题的东西.

And I just stumbled onto something that helped me figure it out.

以防万一有人想知道

在您的Startup.ConfigureServices中

In your Startup.ConfigureServices

services.AddMvc().AddJsonOptions(options =>
                {
                    options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                });

这篇关于ISO UTC DateTime格式作为MVC 6 API响应中的默认json输出格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆