加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Json.Net IsoDateTimeConverter无效

发布时间:2020-12-16 06:48:56 所属栏目:asp.Net 来源:网络整理
导读:我有一个用asp.net mvc 4制作的web应用程序 在Global.asax中,我添加了添加的IsoDateTimeConverter protected void Application_Start(){ AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSetti
我有一个用asp.net mvc 4制作的web应用程序

在Global.asax中,我添加了添加的IsoDateTimeConverter

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(
        new IsoDateTimeConverter { DateTimeFormat = "dd-MM-yyyy hh:mm" });
}

我有一个ActionResult

public ActionResult GetDate()
{
    DateTime dateTime = DateTime.Now;
    return Json(dateTime,JsonRequestBehavior.AllowGet);
}

但是,这个actionResult返回这个:

"/Date(1365060823129)/"

我错过了什么?

解决方法

好吧,我相信你对最近一直在使用的所有“不精确”的命名感到有些困惑. ASP.NET Web API使用JsonFormatter(以及一般的格式化程序).

您没有使用ASP.NET Web API,您正在使用ASP.NET MVC 4(这些是完全分离的技术).

在ASP.NET MVC 4中,序列化逻辑仍在JSONResult内部使用JavaScriptSerializer完成,格式化程序不在此处应用.

如果您想将Json.NET(及其IsoDateTimeConverter)与ASP.NET MVC 4一起使用,您仍然需要创建自己的ActionResult,如here所述.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读