zoukankan      html  css  js  c++  java
  • 修改 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

  • 相关阅读:
    Python修饰器 ,控制授权,通过ini配置文件 使用密钥 给函数限制试用期和过期后试用次数
    excel vba 自定义函数 使用正则表达式提取字符串
    python 值比较判断,np.nan is np.nan 却 np.nan != np.nan ,pandas 单个数据框/单元格 值判断nan
    python 读取中文CSV 'gbk' codec can't decode bytes in position 2-3:illegal multibyte sequence
    python ipython [Errno 22] invalid mode ('rb') or filename 、IDE工作路径
    windows下 python 添加PYTHONPATH 环境变量
    pandas(python2) 读取中文数据,处理中文列名
    qq邮箱 微信提醒不通知
    python :import error
    python推荐淘宝物美价廉商品 2.0
  • 原文地址:https://www.cnblogs.com/lxny/p/7307536.html
Copyright © 2011-2022 走看看