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. C# 使用API检查域用户名和密码是否正确

    添加引用: using System.Runtime.InteropServices; public class VerifyUserByDomain { ; ; ); [DllImport(&quo ...

  2. 终于解决了一个Win7 下 VS 编译的问题,困扰了我好几个月

    用 Win7 一年多了,一直在这个环境下编程,其他都挺好,就是有个问题非常恶心,在VS下编译经常出现进程正在使用,无法覆盖的错误,这个问题我记得博问中以前也有其他人遇到过,也没有解决掉.此问题困扰了我 ...

  3. springCloud组件启动时,提示内部tomcat无法加载

    忘记拷贝当时的异常信息了. 本地启动模块,做测试 大概就是emmble tomcat就是表示内部tomcat无法启动. 出现这样的异常是由于配置中心得代码已经修改,但是没有从git上down下来,然后 ...

  4. MySQL自带的性能压力测试工具mysqlslap详解

    使用语法如下:# mysqlslap [options] 常用参数 [options] 详细说明: --auto-generate-sql, -a 自动生成测试表和数据,表示用mysqlslap工具自 ...

  5. codeforce -39E-What Has Dirichlet Got to Do with That?(博弈+dfs)

    You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1  ...

  6. Java-API-Package:org.springframework.web.bind.annotation

    ylbtech-Java-API-Package:org.springframework.web.bind.annotation 1.返回顶部 1. @NonNullApi @NonNullField ...

  7. mongodb collection method

    https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/ db.coll_test.getIndexes()# ...

  8. DripRoad(点滴之路)

    关于DripRoad DripRoad 意为点滴之路,程序员之路在于点滴积累!是的,这些积累包括技术能力,沟通能力,业务能力等等.   我 我是唐志伟,2009年一个人来上海,就读于上海医疗器械高等专 ...

  9. css3弹性布局语法全解

    本文介绍css3弹性布局的语法 html布局 <div class="box"> <div class="item">1</div ...

  10. findwindow\sendmessage向第三方软件发送消息演示

    这里仅仅是以putty作为演示消息发送机制和控件搜索机制 程序一:代填IP和端口,并建立远程连接 #include"stdafx.h"#include <windows.h& ...