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

  1. WebApi返回Json格式字符串

    WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...

  2. webapi返回json格式优化

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...

  3. webapi返回json格式,并定义日期解析格式

    1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...

  4. (转)WebApi返回Json格式字符串

    原文地址:https://www.cnblogs.com/elvinle/p/6252065.html WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉 ...

  5. WebAPI搭建(二) 让WebAPI 返回JSON格式的数据

    在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...

  6. webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...

  7. WebAPI返回JSON的正确格式

    最近打算用WebAPI做服务端接口,返回JSON供ANDROID程序调用,结果试了好几次JSONObject都无法解析返回的JSON字符串.看了一下服务端代码: public string Get() ...

  8. WebAPI返回JSON

    web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面就介绍两种方法: 方法一:(改配置法) 找到Global.asax文件,在Applic ...

  9. C# WebApi 返回JSON

    在默认情况下,当我们新建一个webapi项目,会自动返回XML格式的数据,如果我们想返回JSON的数据,可以设置下面的三种方法. 1. 不用改配置文件,在Controller的方法中,直接返回Http ...

随机推荐

  1. Unity3D的SystemInfo类,用于获取运行设备硬件信息(CPU、显卡、类型等)

    SystemInfo类中的静态变量:   中文显示: Rendering.CopyTextureSupport copyTextureSupport:(只读)支持多种复制纹理功能的情况. string ...

  2. Genymotion的使用 -- A Faster Android Emulator

    Genymotion 安装与配置 1,Genymotion 模拟器 EditText获取焦点时不自动弹出软件盘 选择该模拟器的设置--> 选中Use Virtual keyboard for t ...

  3. Day3-Python基础3---函数介绍

    一.函数基本语法及特性 函数是什么? 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做sub ...

  4. Day2-Python基础2---字符编码与转码

    详细内容http://www.cnblogs.com/yuanchenqi/articles/5956943.html 一.编码介绍: 1.基本概念: 在python 2中默认编码是 ASCII,而在 ...

  5. Java 8新增的日期、时间格式器

    一 获取DateTimeFormatter对象的三种方式 直接使用静态常量创建DateTimeFormatter格式器 使用代码不同风格的枚举值来创建DateTimeFormatter格式器 根据模式 ...

  6. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  7. 侯捷STL学习(12)--STL相关内容hash+tuple

    layout: post title: 侯捷STL学习(12) date: 2017-08-01 tag: 侯捷STL --- 第四讲 STL相关的内容 Hash Function 将hash函数封装 ...

  8. Java学习之系统高可用性渲染接口日志自动服务降级

    背景:公司都追求系统的高可用性,这里不可用时间就是其中很重要的一个指标,为此在做系统功能升级迭代的过程中如何快速处理异常恢复正常功能极为重要.现在对新增模块的要求是都增加开关,方便快速关闭异常模块,但 ...

  9. VxVM如何扩展和收缩卷及文件系统

    1. 同时扩展卷和文件系统 先用vxassist命令检查DG可用空间 [root@rhelnode1 ~]# vxassist -g testdg maxsize Maximum volume siz ...

  10. 使用 Github 和 Hexo 快速搭建个人博客

    导语 个人兴趣爱好特别广泛,喜欢捣鼓各种小东西自娱自乐.虽然都没能深入研究,但是自己的“孩子”还是很想拿出来遛遛得人一句夸奖的.所以刚学 Markdown 的时候很是有想过要搭个个人博客来玩玩,一来激 ...