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. Python 设计一个简单的计算器

    设计目标 实现加减乘除及拓号优先级解析 用户输入'1 - 2 * ( (6-3 +(-5/5)*(9-2*3/3 + 7/3*7/4*12 +10 * 5/5 )) - (-4*3)/ (12-3*2 ...

  2. Azure CLI的Query

    Azure CLI 2.0是基于Python的命令行.其命令直观,使用非常方便. 其输出有四种模式: --output -o : Output format. Allowed values: json ...

  3. (转)SqlDependency学习笔记

    本文转载自:http://www.cnblogs.com/yjmyzz/archive/2009/06/14/1502921.html sqlDependency提供了这样一种能力:当被监测的数据库中 ...

  4. PostgreSQL 备份和恢复

    备份和恢复有三种不同的基本方法来备份PostgreSQL数据SQL转储文件系统级备份File system level backup连续归档 1. SQL转储 pg_dump dbname > ...

  5. Cassandra 学习三 数据模型

       Cassandra如何存储数据的概述. 集群(Cluster) ·Cassandra数据库分布在几个一起操作的机器上.最外层容器被称为集群.对于故障处理,每个节点包含一个副本,如果发生故障,副本 ...

  6. 基于OpenCV的火焰检测(三)——HSI颜色判据

    上文向大家介绍了如何用最简单的RGB判据来初步提取火焰区域,现在我要给大家分享的是一种更加直观的判据--HSI判据. 为什么说HSI判据是更加直观的判据呢?老规矩,先介绍一下HSI色彩模型: HSI颜 ...

  7. 新手编译开发OpenWrt入门教程(自定义固件、ubuntu学习)

    转自:   http://www.znck007.com/forum.php?mod=viewthread&tid=21571 由于openwrt编译教程资料很多,不同的cpu芯片只需要选择对 ...

  8. Python函数(六)-嵌套函数

    嵌套函数就是在一个函数里再嵌套一个或多个函数 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" def First(): pri ...

  9. ie6-ie8不支持opacity,rgba解决方法

    半透明部分设置样式:opacity:0.7在ie9/ie10/ff/chrome/opera/safari显示正常. 但是这样在ie6-ie8中是不支持的,需要加上下面这句话: filter: pro ...

  10. dp-矩阵连乘

    参考:http://blog.csdn.net/liufeng_king/article/details/8497607 使用备忘录算法复杂度降至O(n^3) #include<stdio.h& ...