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.的更多相关文章

  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. 【前端_js】Chrome禁止缓存的方法

    在前端开发中,浏览器缓存使得我们改了代码后页面不变,得经常手动清理缓存. 1.按如下操作即可禁用浏览器缓存, 这种方法基本能够做到完全禁止缓存,然而缺点是必须要将开发模式一直打开,占用屏幕空间.而且, ...

  2. PHP 工厂模式介绍

    工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么.代码中也类似,把主要参数放入一个工厂里,返回的是处理好的数据,我们并不需要工厂里做了什么,只需要知 ...

  3. 基于python3.7的一个闯越自动签到脚本--demo版

    望指正demo的定位,有时候会抽风无法接受我的定位信息 #! /usr/bin/python3 # -*- coding:UTF- -*- # time : // : # file : chuangy ...

  4. Pycharm中F4查看函数的相关小BUG

    我们都知道在Pycharm中我们要快速查看某个函数或者模块的源码,可以在该函数上按F4快捷键,其可以打开源码相关的.py文件,这两天偶然发现起打开的文件不一定是对的. -代码如下: import os ...

  5. 算法训练 Eurodiffusion

    Eurodiffusion /***********并未完全AC***********/ #include<iostream> #include<algorithm> #inc ...

  6. [Hdu1693]Eat the Trees(插头DP)

    Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...

  7. 问题 1936: [蓝桥杯][算法提高VIP]最大乘积

    问题 1936: [蓝桥杯][算法提高VIP]最大乘积 时间限制: 1Sec 内存限制: 128MB 提交: 77 解决: 16 题目描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? ...

  8. 【Sudoku Solver】cpp

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  9. 【LoadRunner】场景执行报错“failed: WSA_IO_PENDING”

    问题:性能测试场景执行报“failed: WSA_IO_PENDING”错误 解决方法: 添加web_set_sockets_option("OVERLAPPED_SEND", & ...

  10. Pycharm注册码最新版本2019激活码activation code + 最实用的激活方法(亲测有效)

    同时适用于jetbrains全系列可用例:IDEA.WebStorm.phpstor 由于想趁着这个寒假多学习下python,所以这些实用小技巧分享给大家,拿走不谢~ 这里为大家提供了两种最实用的激活 ...