webapi 返回json
web api 默认的已 xml 格式返回数据
现在开发一般都是以 json 格式为主
下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=xml 即可返回 xml 格式数据
配置如下:
1.新建 一个 mvc webapi 项目 (framework4.0)
2.找到默认的 WebApiConfig.cs 文件
3.修改 WebApiConfig.cs 文件
<span style="font-family: Arial, Helvetica, sans-serif;">using System;</span>
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http; namespace MvcWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
....... GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
//默认返回 json
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new QueryStringMapping("datatype", "json", "application/json"));
//返回格式选择 datatype 可以替换为任何参数
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("datatype", "xml", "application/xml"));
}
}
}
4.修改默认路由规则 WebApiConfig.cs 文件中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http; namespace MvcWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//新加的规则
config.Routes.MapHttpRoute(
name: "DefaultApi2",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//新加的规则
config.Routes.MapHttpRoute(
name: "DefaultApi1",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
//默认路由
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
。。。。。
}
}
}
5.添加测试 action
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; namespace MvcWebApi.Controllers
{
public class ValuesController : ApiController
{
/// <summary>
/// web api 默认将以 get 开头的只支持 get 请求,post 开头的支持支 post 请求
/// </summary>
/// <returns></returns>
[System.Web.Http.HttpGet]
[System.Web.Http.HttpPost]
public MyClass GetMyClass()
{
return new MyClass()
{
id=1111,
name="张三",
time=DateTime.Now
};
}
} public class MyClass
{
public int id { set; get; }
public string name { set; get; }
public DateTime time { set; get; }
}
}
6.测试
请求地址:http://localhost:61667/api/values/getmyclass
响应内容:
{"id":1111,"name":"张三","time":"2015-09-29T16:43:07.4731034+08:00"}
请求地址:http://localhost:61667/api/values/getmyclass?datatype=xml
响应内容:
<MyClass><id>1111</id><name>张三</name><time>2015-09-29T16:43:45.3663004+08:00</time></MyClass>
webapi 返回json的更多相关文章
- WebApi返回Json格式字符串
WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...
- webapi返回json格式优化
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...
- webapi返回json格式,并定义日期解析格式
1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...
- (转)WebApi返回Json格式字符串
原文地址:https://www.cnblogs.com/elvinle/p/6252065.html WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉 ...
- WebAPI搭建(二) 让WebAPI 返回JSON格式的数据
在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...
- webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...
- WebAPI返回JSON的正确格式
最近打算用WebAPI做服务端接口,返回JSON供ANDROID程序调用,结果试了好几次JSONObject都无法解析返回的JSON字符串.看了一下服务端代码: public string Get() ...
- WebAPI返回JSON
web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...
- C# WebApi 返回JSON
在默认情况下,当我们新建一个webapi项目,会自动返回XML格式的数据,如果我们想返回JSON的数据,可以设置下面的三种方法. 1. 不用改配置文件,在Controller的方法中,直接返回Http ...
随机推荐
- Python函数-bool()
bool([x]) 作用: 将x转换为Boolean类型,如果x缺省,返回False,bool也为int的子类: 参数x: 任意对象或缺省:大家注意到:这里使用了[x],说明x参数是可有可无的,如果不 ...
- 检测一个DLL文件是x64还是x86
对于一个DLL,我们如何判定其是32位的还是64位的,或者是any cpu的platform? Visual Studio提供了一个很好的工具:corflags,这个是内嵌到Developer Com ...
- 引用com.sencha.gxt.ui.GXT加载错误解决方案
环境GWT2.7+GXT4.0 <inherits name='com.sencha.gxt.ui.GXT' /> 出现加载错误 Loading inherited module 'com ...
- Day3-Python基础3---函数递归和函数式方程
一.函数的递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归特性: 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题规模相比上次递归都应 ...
- win10/server2019 系统安装 详解
https://www.microsoft.com/zh-cn/software-download/windows10 https://go.microsoft.com/fwlink/?LinkId= ...
- 新手编译开发OpenWrt入门教程(自定义固件、ubuntu学习)
转自: http://www.znck007.com/forum.php?mod=viewthread&tid=21571 由于openwrt编译教程资料很多,不同的cpu芯片只需要选择对 ...
- iOS下拉图片放大
效果图 开始简单的代码过程 其实思路很简单 就是 让tableView偏移 一图片的高度,然后在把图片添加到tableView中,然后再监听didScrollView,在里面改变图片的frame - ...
- java selenium webdriver第二讲 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- MySQL在cmd和python下的常用操作
环境配置1:安装mysql,环境变量添加mysql的bin目录 环境配置2:python安装MySQL-Python 请根据自身操作系统下载安装,否则会报c ++ compile 9.0,import ...
- UML在实践中的现状和一些建议
本文是我在csdn上看到的文章,由于认识中的共鸣,摘抄至此. 原文地址:http://blog.csdn.net/vrman/article/details/280157 UML在国内不少地方获得了应 ...