zoukankan      html  css  js  c++  java
  • web api中post参数时只能一个

    在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流。

    举例子说明:

    /?id=123&name=bob 
    void Action(int id, string name) // 所有参数都是简单类型,因而都将来自url

     /?id=123&name=bob 

    void Action([FromUri] int id, [FromUri] string name) // 同上

    void Action([FromBody] string name); //[FormBody]特性显示标明读取整个body为一个字符串作为参数

    public class Customer {   // 定义的一个复杂对象类型 
      public string Name { get; set; } 
      public int Age { get; set; } 
    }

    /?id=123 
    void Action(int id, Customer c) // 参数id从query string中读取,参数c是一个复杂Customer对象类戏,通过formatter从body中读取

    void Action(Customer c1, Customer c2) // 出错!多个参数都是复杂类型,都试图从body中读取,而body只能被读取一次

    void Action([FromUri] Customer c1, Customer c2) // 可以!不同于上面的action,复杂类型c1将从url中读取,c2将从body中读取

    void Action([ModelBinder(MyCustomBinder)] SomeType c) // 标示使用特定的model binder来解析参数

    [ModelBinder(MyCustomBinder)] public class SomeType { } // 通过给特定类型SomeType声明标注[ModelBidner(MyCustomBinder)]特性使得所有SomeType类型参数应用此规则 

    void Action(SomeType c) // 由于c的类型为SomeType,因而应用SomeType上的特性决定其采用model binding

  • 相关阅读:
    case when in sql server's stored procedure
    Hadoop-2.2.0中国文献—— MapReduce 下一代 -- 公平调度
    cocos2d-x游戏循环和日程安排
    归并+高速分拣
    【Nginx】启动过程
    IOS线程操作(3)
    Android最方便的数据库--LitePal
    Android采用Application总结一下
    POJ 2151 Check the difficulty of problems (动态规划-可能DP)
    乞讨N!到底有多少0
  • 原文地址:https://www.cnblogs.com/ywkpl/p/3519434.html
Copyright © 2011-2022 走看看