1.add content negotiator

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Net.Http.Formatting;
  6. using System.Net.Http.Headers;
  7. using System.Web;
  8.  
  9. namespace PtvV2ToolWebApi
  10. {
  11. public class JsonContentNegotiator : IContentNegotiator
  12. {
  13. private readonly JsonMediaTypeFormatter _jsonFormatter;
  14.  
  15. public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
  16. {
  17. _jsonFormatter = formatter;
  18. }
  19.  
  20. public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
  21. {
  22. var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
  23. return result;
  24. }
  25. }
  26. }

2.add below code in app_start folder webapiconfig.cs to register config

  1. using Newtonsoft.Json.Serialization;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Http.Formatting;
  6. using System.Web.Http;
  7.  
  8. namespace PtvV2ToolWebApi
  9. {
  10. public static class WebApiConfig
  11. {
  12. public static void Register(HttpConfiguration config)
  13. {
  14.  
  15. // Remove the XML formatter
  16. config.Formatters.Remove(config.Formatters.XmlFormatter);
  17.  
  18. // Web API configuration and services
  19. var json = config.Formatters.JsonFormatter;
  20. //
  21. json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
  22.  
  23. //
  24. var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
  25. jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  26.  
  27. config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
  28.  
  29. config.Routes.MapHttpRoute(
  30. name: "DefaultApi",
  31. routeTemplate: "api/{controller}/{id}",
  32. defaults: new { id = RouteParameter.Optional }
  33. );
  34.  
  35. // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
  36. // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
  37. // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
  38. //config.EnableQuerySupport();
  39.  
  40. // To disable tracing in your application, please comment out or remove the following line of code
  41. // For more information, refer to: http://www.asp.net/web-api
  42.  
  43. config.EnableSystemDiagnosticsTracing();
  44. }
  45. }
  46. }

at last return list auto change to json

config .net webapi to return json.的更多相关文章

  1. 修改 mvc webapi 默认返回 json 格式

    web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...

  2. 如何让webapi只返回json格式数据

    最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...

  3. .NetCore2.1 WebAPI 根据swagger.json自动生成客户端代码

    前言 上一篇博客中我们可以得知通过Swagger插件可以很方便的提供给接口开发者在线调试,但是实际上Swagger附带的功能还有很多, 比如使用NSwag生成客户端调用代码,进一步解放接口开发者. N ...

  4. ASP.NET WebApi 自带Json返回日期带T无法格式化的问题

    WebApi自带json序列化对遇到时间日期字段的时候,到前端获取的格式总是为“ 2016-07-14T15:32:44”,中间总是会带一个T,显然不是很友好.先是偷懒在园子里边去找一些解决方案,尝试 ...

  5. 最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format

    ABP动态webapi返回的json数据中,日期时间带T还有毫秒数的问题,在以往的版本中可以使用下面方法解决: 在XXXAbpWebApiModule中加上下面的代码: 很老的很老的版本有效: pub ...

  6. VB 老旧版本维护系列---尴尬的webapi访问返回json对象

    尴尬的webapi访问返回json对象 首先Imports Newtonsoft.Json Imports MSXML2(Interop.MSXML2.dll) Dim URLEncode As Sy ...

  7. jq向webApi提交post json数据

    在页面想webApi post json数据的时候,发现webapi不能直接以json的方式接受数据(注:我是没有发现一个很好的方式来post json数据的);但是可以以数据结构的方式传递: 如下: ...

  8. 使用 .net WEBAPI 返回 application/json类型导致 IE8 提示下载

    1, 场景介绍 项目使用了 jquery.form.js这个插件,用 ajax的方式提交 form 表单里面的信息.因为需要使用 ajax获取到返回信息.数据录入使用的是 .net mvc方式的 we ...

  9. WebApi接口返回json,xml,text纯文本等

    [Route("api/Message/MessageList/")] [HttpGet] public HttpResponseMessage MessageList() { R ...

随机推荐

  1. Java - 通过私有构造方法获取实例

  2. 绘制文字:imagettftext()

    <?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...

  3. 数据结构-队列(Queue)

    #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 1 ...

  4. MTCNN学习进展

    20190618 截止今日,学习了MTCNN预测部分的内容,包括三个网络输入输出之类的东西. 之后需要进一步学习的,NMS原理鞋机,MTCNN训练过程细节,损失函数细节

  5. Nosql和RDBMS的比较及解释

    概述 传统的关系型数据库以及数据仓库在面对大数据的处理时显得越来越力不从心.因为关系数据库管理系统 (RDBMS)的设计从未考虑过能够处理日益增长且格式多变的数据,以及访问数据并进行分析的用户需求呈爆 ...

  6. Missian指南三:创建一个Missian服务器(使用spring)

    在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用.Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗. Missian在服务器端依赖与Mi ...

  7. Flask学习笔记:数据库ORM操作MySQL+pymysql/mysql-python+SQLAlchemy/Flask-SQLAlchemy

    Python中使用sqlalchemy插件可以实现ORM(Object Relationship Mapping,模型关系映射)框架,而Flask中的flask-sqlalchemy其实就是在sqla ...

  8. 添加SQL字段

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] add 字段名 smallin ...

  9. linux lvm扩容

    1.分区,  查看磁盘使用:fdisk -l 对磁盘分区:fdisk /dev/sdb 2.创建pv pvcreate /dev/sdb1 查看pv: pvdisplay 3.查看vg  vgdisp ...

  10. 记忆化搜索:POJ1579-Function Run Fun(最基础的记忆化搜索)

    Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14815 Accepted: 7659 Des ...