1、返回json 修改App_Start/webapiconfig

public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}

2、配置路由。在controller上面加filter  [RoutePrefix("api/EquipmentApi")],在action上面加 [Route("GetEquipmentsOfStoreHouse")]

则访问时用http://aaaaaa/api/EquipmentApi/GetEquipmentsOfStoreHouse?begin=0&pagesize=8&stateId=1

[RoutePrefix("api/EquipmentApi")]
public class EquipmentApiController : ApiController
{

[Route("GetEquipmentsOfStoreHouse")]
[HttpGet]
public ApiResponse<List<EquipmentViewModel>> GetEquipments(int begin, int pageSize, byte? stateId = null)
{

};
}

3、如上面的action参数默认值,则不用传stateId这个参数,http://aaaaaa/api/EquipmentApi/GetEquipmentsOfStoreHouse?begin=0&pagesize=8

webapi 返回json及route设置的更多相关文章

  1. webapi返回json格式优化

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 config.Formatters.Remove(config.For ...

  2. WebAPI搭建(二) 让WebAPI 返回JSON格式的数据

    在RestFul风格盛行的年代,对接接口大多数人会选择使用JSON,XML和JSON的对比传送(http://blog.csdn.net/liaomin416100569/article/detail ...

  3. webapi返回json格式优化 转载https://www.cnblogs.com/GarsonZhang/p/5322747.html

    一.设置webapi返回json格式 在App_Start下的WebApiConfig的注册函数Register中添加下面这代码 1 config.Formatters.Remove(config.F ...

  4. WebApi返回Json格式字符串

    WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉都不怎么好. 先贴一下, 网上给的常用方法吧. 方法一:(改配置法) 找到Global.asax文件,在 ...

  5. webapi返回json格式,并定义日期解析格式

    1.webapi返回json格式 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferen ...

  6. (转)WebApi返回Json格式字符串

    原文地址:https://www.cnblogs.com/elvinle/p/6252065.html WebApi返回json格式字符串, 在网上能找到好几种方法, 其中有三种普遍的方法, 但是感觉 ...

  7. asp.net webapi 返回json结果的方法

    第一种: public static void Register(HttpConfiguration config) { //1.将默认的xml格式化程序清除 GlobalConfiguration. ...

  8. C# WebApi 返回JSON

    在默认情况下,当我们新建一个webapi项目,会自动返回XML格式的数据,如果我们想返回JSON的数据,可以设置下面的三种方法. 1. 不用改配置文件,在Controller的方法中,直接返回Http ...

  9. WebAPI返回JSON的正确格式

    最近打算用WebAPI做服务端接口,返回JSON供ANDROID程序调用,结果试了好几次JSONObject都无法解析返回的JSON字符串.看了一下服务端代码: public string Get() ...

随机推荐

  1. 九度OJ小结

    1. 高精度问题 可参考题目 题目1137:浮点数加法   http://ac.jobdu.com/problem.php?pid=1137 对于高精度问题可以考虑使用结构体.上述为浮点数加法,因此该 ...

  2. innodb 锁机制

    InnoDB锁问题 InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我 ...

  3. Unity3D笔记 英保通五 鼠标事件与GUI系统双击检测

    一.如何使用GUI事件来检测鼠标是否按下的事件: 获取当前事件:var e:Event=Event.current: using UnityEngine; using System.Collectio ...

  4. Power Shell 学习笔记

    Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境. 桌面右击任务栏开始图标,打开控制台对话窗: Windows PowerShell ISE 应用程序的文 ...

  5. Shell if else

    语句1if [ expression ]then ...fi 语句2if [ expression ]then ...else ...fi 语句3if [ expression 1 ]then ... ...

  6. 有向图的强联通tarjan算法(判断是否为强联通模板)(hdu1269)

    hdu1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  7. Docker 学习应用篇之一: 初识Docker

    Docker 自从2013年以来就非常的火热,无论是从github上的代码活跃度,还是Redhat在RHE6.5中集成对Docker的支持,等等.第一次接触Docker,还是老师给我们介绍的. 1.初 ...

  8. Ubuntu 16.04 ORB_SLAM2+ROS+usb_cam+AR

    Ubuntu 16.04 ORB_SLAM2+ROS+usb_cam+AR 参考博文:http://blog.csdn.net/u79501/article/details/68942174 http ...

  9. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  10. First normal formal Second normal form

    https://en.wikipedia.org/wiki/First_normal_form https://en.wikipedia.org/wiki/Second_normal_form A r ...