config .net webapi to return json.
1.add content negotiator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web; namespace PtvV2ToolWebApi
{
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter; public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
} public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}
}
2.add below code in app_start folder webapiconfig.cs to register config
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http; namespace PtvV2ToolWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{ // Remove the XML formatter
config.Formatters.Remove(config.Formatters.XmlFormatter); // Web API configuration and services
var json = config.Formatters.JsonFormatter;
//
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; //
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport(); // To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api config.EnableSystemDiagnosticsTracing();
}
}
}
at last return list auto change to json
config .net webapi to return json.的更多相关文章
- 修改 mvc webapi 默认返回 json 格式
web api 默认的已 xml 格式返回数据 现在开发一般都是以 json 格式为主 下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=x ...
- 如何让webapi只返回json格式数据
最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...
- .NetCore2.1 WebAPI 根据swagger.json自动生成客户端代码
前言 上一篇博客中我们可以得知通过Swagger插件可以很方便的提供给接口开发者在线调试,但是实际上Swagger附带的功能还有很多, 比如使用NSwag生成客户端调用代码,进一步解放接口开发者. N ...
- ASP.NET WebApi 自带Json返回日期带T无法格式化的问题
WebApi自带json序列化对遇到时间日期字段的时候,到前端获取的格式总是为“ 2016-07-14T15:32:44”,中间总是会带一个T,显然不是很友好.先是偷懒在园子里边去找一些解决方案,尝试 ...
- 最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format
ABP动态webapi返回的json数据中,日期时间带T还有毫秒数的问题,在以往的版本中可以使用下面方法解决: 在XXXAbpWebApiModule中加上下面的代码: 很老的很老的版本有效: pub ...
- VB 老旧版本维护系列---尴尬的webapi访问返回json对象
尴尬的webapi访问返回json对象 首先Imports Newtonsoft.Json Imports MSXML2(Interop.MSXML2.dll) Dim URLEncode As Sy ...
- jq向webApi提交post json数据
在页面想webApi post json数据的时候,发现webapi不能直接以json的方式接受数据(注:我是没有发现一个很好的方式来post json数据的);但是可以以数据结构的方式传递: 如下: ...
- 使用 .net WEBAPI 返回 application/json类型导致 IE8 提示下载
1, 场景介绍 项目使用了 jquery.form.js这个插件,用 ajax的方式提交 form 表单里面的信息.因为需要使用 ajax获取到返回信息.数据录入使用的是 .net mvc方式的 we ...
- WebApi接口返回json,xml,text纯文本等
[Route("api/Message/MessageList/")] [HttpGet] public HttpResponseMessage MessageList() { R ...
随机推荐
- ubuntu18.04 and Linux mint 19安装virtualbox
1.1 安装Virtualbox root@amarsoft-ZHAOYANG-K43c-:~# apt-get install virtualbox -y 1.2 显示Virtualbox桌面图 ...
- MySQL批量插入大量数据方法
在MySQL数据库中,如果要插入上百万级的记录,用普通的insert into来操作非常不现实,速度慢人力成本高,推荐使用Load Data或存储过程来导入数据,我总结了一些方法分享如下,主要基于My ...
- python函数调用顺序、高阶函数、嵌套函数、闭包详解
一:函数调用顺序:其他高级语言类似,Python 不允许在函数未声明之前,对其进行引用或者调用错误示范: def foo(): print 'in the foo' bar() foo() 报错: i ...
- c语言——字符串
char str[] = "http://c.biancheng.net"; char str[] = "http://c.biancheng.net"; ch ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- HDU 5657 CA Loves Math 状压DP + 枚举
题意: 给出\(A(2 \leq A \leq 11), n(0 \leq n \leq 10^9), k(1 \leq k \leq 10^9)\). 求区间\([1, A^n]\)中各个数字互不相 ...
- 某面试公司出的面试题---用JS比较两个版本号高低
一天中午某个公司给我反馈的面试题,说,比较两个文件的版本号,然后我给发过去了,说我的代码不符合他的要求,o(╯□╰)o了var compareVersion = compareVersion||fun ...
- SEO搜索引擎优化基础
要如何提高自己网站的知名度,那必须了解一些SEO知识. 1.什么是搜索引擎 所谓的搜索引擎(Search Engines)是一些能够主动搜索信息(搜索网页上的单词和简短的特定的内容描述)并将其自动索 ...
- 使用 CommandScene 类在 XNA 中创建命令场景(十二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 利用js阻止表单提交
(1) return false <form name="loginForm" action="login.aspx" method="post ...