修改 mvc webapi 默认返回 json 格式

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

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

配置如下:

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

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

3.修改 WebApiConfig.cs 文件

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

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

5.添加测试 action
[csharp] view plain copy
 
  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.   
  8. namespace MvcWebApi.Controllers  
  9. {  
  10.     public class ValuesController : ApiController  
  11.     {  
  12.         /// <summary>  
  13.         /// web api 默认将以 get 开头的只支持 get 请求,post 开头的支持支 post 请求  
  14.         /// </summary>  
  15.         /// <returns></returns>  
  16.         [System.Web.Http.HttpGet]  
  17.         [System.Web.Http.HttpPost]  
  18.         public MyClass GetMyClass()  
  19.         {  
  20.             return new MyClass()  
  21.             {  
  22.                 id=1111,  
  23.                 name="张三",  
  24.                 time=DateTime.Now  
  25.             };  
  26.         }  
  27.     }  
  28.   
  29.     public class MyClass  
  30.     {  
  31.         public int id { set; get; }  
  32.         public string name { set; get; }  
  33.         public DateTime time { set; get; }  
  34.     }  
  35. }  

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

原文地址:https://www.cnblogs.com/lxny/p/7307536.html