zoukankan      html  css  js  c++  java
  • webapi 返回json及route设置

    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



  • 相关阅读:
    最近想做的开发配套工具
    nodejs 入门
    MySQL Server 5.6 配置文件my.ini 以及windows上mysql表名区分大小写
    js闭包实例汇总
    javascript深入理解js闭包
    jQuery中$.fn的用法示例介绍
    css3图片旋转
    JS中的prototype
    追求极致--纯css制作三角、圆形按钮,兼容ie6
    CSS 最核心的几个概念
  • 原文地址:https://www.cnblogs.com/taoshengyujiu/p/6077052.html
Copyright © 2011-2022 走看看