修改 mvc webapi 默认返回 json 格式
web api 默认的已 xml 格式返回数据
现在开发一般都是以 json 格式为主
下面配置让 webapi 默认返回 json ,在需要返回 xml 时只需要加一个查询参数 datatype=xml 即可返回 xml 格式数据
配置如下:
1.新建 一个 mvc webapi 项目 (framework4.0)
2.找到默认的 WebApiConfig.cs 文件
3.修改 WebApiConfig.cs 文件
- 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>
转载来源:http://blog.csdn.net/xxj_jing/article/details/48808099
修改 mvc webapi 默认返回 json 格式的更多相关文章
- 如何让webapi只返回json格式数据
最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...
- asp.net MVC控制器中返回JSON格式的数据时提示下载
Asp.net mvc在接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如下: 视图中js代码: $("# ...
- 在IE中MVC控制器中返回JSON格式的数据时提示下载
最近做项目时,视图中用jquery.form.js异步提交表单时,接收的是JSON格式的数据,但是奇怪的是在IE中提示下载文件,其他浏览器中一切正常,下载后,里面的内容就是在控制器中返回的数据.代码如 ...
- webapi返回json格式,并定义日期解析格式
1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...
- WebApi返回Json格式字符串
WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...
- (转)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 ...
- ASP.NET API(MVC) 对APP接口(Json格式)接收数据与返回数据的统一管理
话不多说,直接进入主题. 需求:基于Http请求接收Json格式数据,返回Json格式的数据. 整理:对接收的数据与返回数据进行统一的封装整理,方便处理接收与返回数据,并对数据进行验证,通过C#的特性 ...
- webapi返回json格式优化
一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...
随机推荐
- JavaSE编码试题强化练习1
1. 编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 定义父类--圆类 */ public class Circle { / ...
- 吉首大学2019年程序设计竞赛-F 天花乱坠
题目链接:https://ac.nowcoder.com/acm/contest/992/F 题意:给定正n边形,边长为100,以每条边的中点连线构成新的正n边形,无限循环下去,求所有边的长度和. 思 ...
- Flood Fill
An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...
- ZooKeeper的ACL权限
ACL控制权限 什么是ACL(Access Control List访问控制列表) 针对节点可以设置相关读写等权限, 目的为了保障数据安全性 权限permission可以指定不同的权限范围以及角色 A ...
- Luogu P5068 [Ynoi2015]我回来了
题目 Ynoi难得的水题. 首先我们可以\(O(n^2)\)地求出任意两点之间的距离. 然后我们可以\(O(n^3)\)地求出对于任意一个点\(u\),跟它距离\(\le d\)的点的集合. 然后对于 ...
- python-优酷系统管理员视图粗糙版(无详细注释)
目录 Tank-YouKu(仅管理员功能粗糙版) 优酷系统管理员视图功能 前期准备 创库创表语句 安装pymysql模块 安装DBUtils模块 配置 db_pool 项目架构与数据流向 目录结构 s ...
- leecode刷题(30)-- 二叉树的后序遍历
leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...
- mybaits 在test判断数字,或者数字型字符串时注意事项
1.在test中判断传入值为0的Integer或者Long时,mybaits会将其视为null 解决方法: 把Integer/Long改为String类型. status!=null and stat ...
- 配置Mysql远程连接
一.赋予某个用户权限 1.赋予权限格式:grant 权限 on 数据库对象 to 用户@IP(或者相应正则) 注:可以赋予select,delete,update,insert,index等权限精确到 ...
- java中的异常和处理详细理解
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用System.ou ...