web api 默认的已 xml 格式返回数据

现在开发一般都是以 json 格式为主

下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=xml 即可返回 xml 格式数据

配置如下:

1.新建 一个 mvc webapi 项目 (framework4.0)

2.找到默认的 WebApiConfig.cs 文件

3.修改 WebApiConfig.cs 文件

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Net.Http.Formatting;
  4. using System.Web.Http;
  5. namespace MvcWebApi
  6. {
  7. public static class WebApiConfig
  8. {
  9. public static void Register(HttpConfiguration config)
  10. {
  11. .......
  12. GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
  13. //默认返回 json
  14. GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
  15. new QueryStringMapping("datatype", "json", "application/json"));
  16. //返回格式选择 datatype 可以替换为任何参数
  17. GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
  18. new QueryStringMapping("datatype", "xml", "application/xml"));
  19. }
  20. }
  21. }

4.修改默认路由规则 WebApiConfig.cs 文件中

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http.Formatting;
  5. using System.Web.Http;
  6. namespace MvcWebApi
  7. {
  8. public static class WebApiConfig
  9. {
  10. public static void Register(HttpConfiguration config)
  11. {
  12. //新加的规则
  13. config.Routes.MapHttpRoute(
  14. name: "DefaultApi2",
  15. routeTemplate: "api/{controller}/{action}/{id}",
  16. defaults: new { id = RouteParameter.Optional }
  17. );
  18. //新加的规则
  19. config.Routes.MapHttpRoute(
  20. name: "DefaultApi1",
  21. routeTemplate: "api/{controller}/{action}",
  22. defaults: new { id = RouteParameter.Optional }
  23. );
  24. //默认路由
  25. config.Routes.MapHttpRoute(
  26. name: "DefaultApi",
  27. routeTemplate: "api/{controller}/{id}",
  28. defaults: new { id = RouteParameter.Optional }
  29. );
  30. 。。。。。
  31. }
  32. }
  33. }

5.添加测试 action

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. namespace MvcWebApi.Controllers
  8. {
  9. public class ValuesController : ApiController
  10. {
  11. /// <summary>
  12. /// web api 默认将以 get 开头的只支持 get 请求,post 开头的支持支 post 请求
  13. /// </summary>
  14. /// <returns></returns>
  15. [System.Web.Http.HttpGet]
  16. [System.Web.Http.HttpPost]
  17. public MyClass GetMyClass()
  18. {
  19. return new MyClass()
  20. {
  21. id=1111,
  22. name="张三",
  23. time=DateTime.Now
  24. };
  25. }
  26. }
  27. public class MyClass
  28. {
  29. public int id { set; get; }
  30. public string name { set; get; }
  31. public DateTime time { set; get; }
  32. }
  33. }

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>

转载来源:http://blog.csdn.net/xxj_jing/article/details/48808099

修改 mvc webapi 默认返回 json 格式的更多相关文章

  1. 如何让webapi只返回json格式数据

    最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...

  2. asp.net MVC控制器中返回JSON格式的数据时提示下载

    Asp.net mvc在接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如下: 视图中js代码: $("# ...

  3. 在IE中MVC控制器中返回JSON格式的数据时提示下载

    最近做项目时,视图中用jquery.form.js异步提交表单时,接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如 ...

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

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

  5. WebApi返回Json格式字符串

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

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

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

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

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

  8. ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理

    话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...

  9. webapi返回json格式优化

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

随机推荐

  1. Oracle密码过期(the password has expired)

    1.进入sqlplus模式--sqlplus / as sysdba; 2.查看用户密码的有效期设置(一般默认的配置文件是DEFAULT) SELECT * FROM dba_profiles WHE ...

  2. [转帖]天津飞腾回应处理器造假 没有采用ARM的内核,内核自主设计

    天津飞腾回应处理器造假没有采用ARM的内核,内核自主设计 ... https://www.expreview.com/63233.html 看了下 同意孟宪瑞老师的关系 飞腾 的确改动了 ARM的架构 ...

  3. (4.15)mysql备份还原——物理备份之XtraBackup的下载与安装

    关键词:mysql物理备份,XtraBackup,XtraBackup安装,XtraBackup下载 实践链接:https://www.cnblogs.com/gered/p/11147193.htm ...

  4. 跨 PostgreSQL 大版本复制怎么做?| 逻辑复制

    当需要升级PostgreSQL时,可以使用多种方法.为了避免应用程序停机,不是所有升级postgres的方法都适合,如果避免停机是必须的,那么可以考虑使用复制作为升级方法,并且根据方案,可以选择使用逻 ...

  5. pwd命令和修改PS1环境变量在bash行的显示

    一.pwd:显示当前所在的位置 语法 pwd [选项] ... 描述       打印当前工作目录的完整文件名. -L,--logical              从环境使用PWD,即使它包含符号链 ...

  6. 使用Python基于OpenCV的验证码识别

    Blog:https://blog.csdn.net/qq_40962368/article/details/89312429(Verification_Code_Identification) 步骤 ...

  7. 搭建集群版Eureka Server

    注册中心作为微服务架构中的核心功能,其重要性不言而喻.所以单机版的Eureka Server在可靠性上并不符合现在的互联网开发环境.集群版的Eureka Server才是商业开发中的选择. Eurek ...

  8. 01 Mysql数据库初识

    一.数据库概述 1.什么是数据库? 什么是数据库呢? 先来看看百度怎么说的 数据库,简而言之可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据运行新增.截取.更新.删除等操作. 所谓“ ...

  9. 上海的Costco,谈谈你的理解和感受

    众所周知,Costco在上海第一天开业,由于人流量过大,一度暂停营业.我觉得Costco的成功在于不走寻常路,换位思考(站在用户.厂商角度看问题),下面几点是我觉得它做得比较独特的地方: 1. Cos ...

  10. Caffe之prototxt

    1.可视化工具: http://ethereon.github.io/netscope/quickstart.html 2.常用网络模型caffe-model之.prototxt: https://g ...