编程语言
首页 > 编程语言> > C#WebApi返回时间带T解决实例

C#WebApi返回时间带T解决实例

作者:互联网

1,WebApiConfig.cs加上如下代码

        public static void ConfigureApiFormatter(HttpConfiguration config)
        {
            var jsonFormatter = new JsonMediaTypeFormatter();
            var settings = jsonFormatter.SerializerSettings;
            //驼峰
            //settings.ContractResolver = new CamelCasePropertyNamesContractResolver();            

            //同一时间处理
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            //这里使用自定义日期格式
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
            settings.Converters.Add(timeConverter);

            config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
        }

  2,新建一个类 名称为JsonContentNegotiator 加入如下代码

    public class JsonContentNegotiator: IContentNegotiator
    {

        private JsonMediaTypeFormatter jsonFormatter;

        public JsonContentNegotiator(JsonMediaTypeFormatter jsonFormatter)
        {
            this.jsonFormatter = jsonFormatter;
        }

        public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
        {
            var result = new ContentNegotiationResult(jsonFormatter, new MediaTypeHeaderValue("application/json"));
            return result;
        }
    }

 

标签:WebApi,C#,jsonFormatter,实例,var,new,JsonMediaTypeFormatter,JsonContentNegotiator,
来源: https://www.cnblogs.com/xuxian001/p/12882902.html