joyswings

执行dotnet-new selfhost sstest 创建项目,然后打开解决方案

修改ssTest.ServiceModel中的Hello.cs,在HellopResponse中添加时间属性,然后修改MyServices中的代码

运行程序,打开Postman查看返回结果

 

 

 可以看到Json中date属性返回的是  "date": "/Date(1555810731732+0800)/",直接导致前段js无法识别该字段,该如何解决?

在Startup中加入以下代码,任何时间都转换为ISO8601字符串

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            JsConfig<DateTime>.SerializeFn = time => new DateTime(time.Ticks, DateTimeKind.Local).ToString("o");
            JsConfig<DateTime?>.SerializeFn =
                time => time != null ? new DateTime(time.Value.Ticks, DateTimeKind.Local).ToString("o") : null;
            JsConfig.DateHandler = DateHandler.ISO8601;

            app.UseServiceStack(new AppHost());

            app.Run(context =>
            {
                context.Response.Redirect("/metadata");
                return Task.FromResult(0);
            });
        }
    }

 

打开Postman再次运行,查看结果

 

前段js再次取得该字符串时间,就可以正确的识别时间类型字段了。

 

相关文章:

  • 2021-06-05
  • 2022-02-11
  • 2021-09-26
  • 2021-12-22
  • 2021-08-11
  • 2021-07-15
猜你喜欢
  • 2021-11-26
  • 2021-04-19
  • 2021-08-17
  • 2021-11-26
  • 2021-06-14
  • 2021-07-15
  • 2021-05-27
相关资源
相似解决方案