一个很实用的技巧,可以在访问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)的更多相关文章

  1. api接口写好了?想过(Accept,Content-Type)?返回类型json|xml?

    api接口写好了?想过(Accept,Content-Type)?返回类型json|xml? 起因: - A,B. A调用B提供的api接口. - A:为毛你的接口返回的是xml格式的(浏览器访问)? ...

  2. ASP.NET Web API 如何通过程序控制返回xml还是json

    雖然 ASP.NET Web API 內建支援 JSON 與 XML 兩種輸出格式,並依據瀏覽器端送出的 Accept 標頭自動決定回應的內容格式,不過有時候我們的確也需要讓程式來控制要回應哪種格式, ...

  3. asp.net web api [FromBody]参数

    Using jQuery to POST [FromBody] parameters to Web API 时间2013-04-04 00:28:17 Encosia原文 http://encosia ...

  4. Ajax返回类型JSON,XML

    Ajax的三种返回类型 **一.TEXT *二.JSON 数据显示页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  5. ASP.NET Web API默认支持的媒体类型(SupportedMediaTypes)

    JsonMediaTypeFormatter XmlMediaTypeFormatter ( application/xml  text/xml) FormUrlEncodedMediaTypeFor ...

  6. ASP.NET Web API 数据提供系统相关类型及其关系

  7. 【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 ...

  8. HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象

    HttpActionDescriptor,ASP.NET Web API又一个重要的描述对象 通过前面对“HttpController的激活”的介绍我们已经知道了ASP.NET Web API通过Ht ...

  9. Web API 2 入门——使用ASP.NET Web API和Angular.js构建单页应用程序(SPA)(谷歌翻译)

    在这篇文章中 概观 演习 概要 由网络营 下载网络营训练包 在传统的Web应用程序中,客户机(浏览器)通过请求页面启动与服务器的通信.然后,服务器处理请求,并将页面的HTML发送给客户端.在与页面的后 ...

随机推荐

  1. [pod install] error: cannot open .git/FETCH_HEAD: Permission denied

    pod installAnalyzing dependencies[!] Pod::Executable pull error: cannot open .git/FETCH_HEAD: Permis ...

  2. Cocos2d-x 坑之一:Xcode文件真实目录与工程视图目录

    Cocos2d-x一定要保证 Xcode文件真实目录与工程视图目录 的一致性,不然,会出现文件读取不了,或include不了的情况. 如果出现此类情况,优先查看真实目录的结构.

  3. Ubuntu----1

    1. 安装ubuntu之后,你必须要做的事情, 引自:http://itsfoss.com/things-to-do-after-installing-ubuntu-13-04/ 但是对于国人来讲,墙 ...

  4. Yii - 验证和授权(Authentication and Authorization)

    1. 定义身份类 (Defining Identity Class)  为了验证一个用户,我们定义一个有验证逻辑的身份类.这个身份类实现[IUserIdentity] 接口.不同的类可能实现不同的验证 ...

  5. poj 2406 Power Strings【最小循环节】

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36926   Accepted: 15254 D ...

  6. Linux安装Team Service Agent

    (1)下载linux agent文件(在windows中下载后,通过WinSCP复制至linux服务器中) 或者可以在linux直接下载文件(直接下载不会因为网络问题而导致传输中断) 首先使用命令建立 ...

  7. 图片上传unexpected end of stream

    第二次上传头像图片的时候出现 unexpected end of stream 解决办法: 将第一次图片上传成功后做以下操作即可: mBitmapFile.delete(); mBitmapFile ...

  8. 记录:Ubuntu下安装mysql

    >>更新源列表 在终端中输入:sudo apt-get update 更新完成后输入:sudo apt-get install mysql-server mysql-client 接着输入 ...

  9. angular2自学笔记---官网项目(一)

    1.单向数据绑定的'插值表达式' angular中最典型的数据显示方式:把HTML模板(template)的控件绑定到angular组件的属性(component相当于一个构造函数,下面例子中的这个构 ...

  10. SAP HANA STRING_AGG

    HANA Version 1.00.73.00.389160 不支持STRING_AGG,所以只能,,,,,,,, DROP PROCEDURE ""."ZCONCAT_ ...