ASP.NET Web API 通过参数控制返回类型(JSON|XML)
一个很实用的技巧,可以在访问web api服务的时候指定返回数据的格式类型,比如 json 或者 xml。
因为 web api 默认返回的是XML格式,但是现在json 比较流行,同时网上也有其他的全局方式的设置。
比如:
在 Global 文件中设置,首先清除其他所有的formatters,然后只保留JsonMediaTypeFormatter。
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());
实现步骤:
1、在 WebApiConfig 文件中引用:System.Net.Http.Formatting
2、修改 WebApiConfig 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http; namespace Caixie.Dispatcher
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml"); // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controlaler}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
3、访问
http://localhost:31591/api/your-action?$format=json
ASP.NET Web API 通过参数控制返回类型(JSON|XML)的更多相关文章
- api接口写好了?想过(Accept,Content-Type)?返回类型json|xml?
api接口写好了?想过(Accept,Content-Type)?返回类型json|xml? 起因: - A,B. A调用B提供的api接口. - A:为毛你的接口返回的是xml格式的(浏览器访问)? ...
- ASP.NET Web API 如何通过程序控制返回xml还是json
雖然 ASP.NET Web API 內建支援 JSON 與 XML 兩種輸出格式,並依據瀏覽器端送出的 Accept 標頭自動決定回應的內容格式,不過有時候我們的確也需要讓程式來控制要回應哪種格式, ...
- asp.net web api [FromBody]参数
Using jQuery to POST [FromBody] parameters to Web API 时间2013-04-04 00:28:17 Encosia原文 http://encosia ...
- Ajax返回类型JSON,XML
Ajax的三种返回类型 **一.TEXT *二.JSON 数据显示页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- ASP.NET Web API默认支持的媒体类型(SupportedMediaTypes)
JsonMediaTypeFormatter XmlMediaTypeFormatter ( application/xml text/xml) FormUrlEncodedMediaTypeFor ...
- ASP.NET Web API 数据提供系统相关类型及其关系
- 【ASP.NET Web API教程】1.1 第一个ASP.NET Web API
Your First ASP.NET Web API (C#)第一个ASP.NET Web API(C#) By Mike Wasson|January 21, 2012作者:Mike Wasson ...
- HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象
HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象 通过前面对“HttpController的激活”的介绍我们已经知道了ASP.NET Web API通过Ht ...
- Web API 2 入门——使用ASP.NET Web API和Angular.js构建单页应用程序(SPA)(谷歌翻译)
在这篇文章中 概观 演习 概要 由网络营 下载网络营训练包 在传统的Web应用程序中,客户机(浏览器)通过请求页面启动与服务器的通信.然后,服务器处理请求,并将页面的HTML发送给客户端.在与页面的后 ...
随机推荐
- BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...
- ☀【CSS3】icon
Navicon Transformicons: Animated Navigation Icons with CSS Transformshttp://sarasoueidan.com/blog/na ...
- 20个可以帮你简化iOS app开发流程的工具
这里推荐20个可以帮你简化iOS app开发流程的工具.很多开发者都使用过这些工具,涉及原型和设计.编程.测试以及最后的营销,基本上涵盖了整个开发过程. 原型和设计 有了一个很好的创意后,你要做的不是 ...
- A. Difference Row
A. Difference Row time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- apache的FileUtils方法大全
FileUtils 获取系统的临时目录路径:getTempDirectoryPath() [java] view plaincopyprint? public static String getTem ...
- jquery input只允许输入数字
$('#id').keyup(function(){ var tmptxt=$(this).val().replace(/\D|^0/g,''); $(this).val(tmptxt); });
- wxWidgets学习笔记——在屏幕上画简单的图形和文字
在屏幕上画简单图形和显示图片.处理简单鼠标键盘事件 /*************************************************************** * Name: M ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- JAVA Web项目的编码过滤器
首先写一个EncodeFilter的过滤类: package com.djtu.wy.common; import java.io.IOException;import javax.servlet.F ...
- Linux常用命令及vim的使用、vim常用插件(推荐)
看了3篇文章,很好 vim中的区域拷贝 剪切,粘贴: 正常模式,移动光标到剪切的区域开始处,按v,进入可视模式,然后选择区域.按x键,剪切.到指定位置按p粘贴. 撤销按u 恢复按ctrl-r 简明 ...