1. 实例

  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. using wxweb.Areas.API.Models;
  8.  
  9. namespace wxweb.Areas.API.Controllers
  10. {
  11. public class ProductController : ApiController
  12. {
  13. Product[] products = new Product[]
  14. {
  15. new Product { Id = , Name = "Tomato Soup", Category = "Groceries", Price = },
  16. new Product { Id = , Name = "Yo-yo", Category = "Toys", Price = 3.75M },
  17. new Product { Id = , Name = "Hammer", Category = "Hardware", Price = 16.99M }
  18. };
  19.  
  20. Product[] products_post = new Product[]
  21. {
  22. new Product { Id = , Name = "name01", Category = "Category01", Price = },
  23. new Product { Id = , Name = "name02", Category = "Category02", Price = 20M },
  24. new Product { Id = , Name = "name03", Category = "Category03", Price = 30M }
  25. };
  26.  
  27. /// <summary>
  28. /// get 无参数传参
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet]
  32. public IEnumerable<Product> GetAllProducts()
  33. {
  34. return products;
  35. }
  36.  
  37. /// <summary>
  38. /// get 单参数传参
  39. /// </summary>
  40. /// <param name="id"></param>
  41. /// <returns></returns>
  42. [Route("api/Product/GetProductById")]
  43. [HttpGet]
  44. public IHttpActionResult GetProductById(string id)
  45. {
  46. var product = products.FirstOrDefault((p) => p.Id == Convert.ToInt32(id));
  47. if (product == null)
  48. {
  49. return NotFound();
  50. }
  51. return Ok(product);
  52. }
  53.  
  54. /// <summary>
  55. /// get多参数传参
  56. /// </summary>
  57. /// <param name="id"></param>
  58. /// <param name="name"></param>
  59. /// <returns></returns>
  60. [HttpGet]
  61. public string para_get_base(string id,string name)
  62. {
  63. return "id:"+id+" name:"+name;
  64. }
  65.  
  66. /// <summary>
  67. /// get form传参
  68. /// </summary>
  69. /// <param name="p"></param>
  70. /// <returns></returns>
  71. [Route("api/Product/para_get_form")]
  72. [HttpGet]
  73. public string para_get_form([FromUri]Product p)
  74. {
  75. return "p.Name:" + p.Name + " p.Price:" + p.Price;
  76. }
  77.  
  78. /// <summary>
  79. /// post 无参数传参
  80. /// </summary>
  81. /// <returns></returns>
  82. [Route("api/Product/GetProducts")]
  83. [HttpPost]
  84. public IEnumerable<Product> GetProducts()
  85. {
  86.  
  87. return products_post;
  88. }
  89.  
  90. /// <summary>
  91. /// post 单个参数传参
  92. /// </summary>
  93. /// <param name="id"></param>
  94. /// <returns></returns>
  95. [Route("api/Product/GetProduct")]
  96. [HttpPost]
  97. public IHttpActionResult GetProduct([FromBody]string id)
  98. {
  99. var product = products_post.FirstOrDefault((p) => p.Id ==Convert.ToInt32( id));
  100. if (product == null)
  101. {
  102. return NotFound();
  103. }
  104. return Ok(product);
  105. }
  106. /// <summary>
  107. /// post 多参数传参
  108. /// </summary>
  109. /// <param name="obj"></param>
  110. /// <returns></returns>
  111. [Route("api/Product/para_post_base")]
  112. [HttpPost]
  113. public string para_post_base(dynamic obj)
  114. {
  115. string id = obj["id"].ToString();
  116. string name = obj["name"].ToString();
  117. return "id:" + id + " name:" + name;
  118. }
  119.  
  120. /// <summary>
  121. /// post form传参
  122. /// </summary>
  123. /// <param name="p"></param>
  124. /// <returns></returns>
  125. [Route("api/Product/para_post_form")]
  126. [HttpPost]
  127. public string para_post_form(Product p)
  128. {
  129. return "p.Name:" + p.Name + " p.Price:" + p.Price;
  130. }
  131.  
  132. /// <summary>
  133. /// post base+form传参
  134. /// </summary>
  135. /// <param name="p"></param>
  136. /// <returns></returns>
  137. [Route("api/Product/para_post_baseform")]
  138. [HttpPost]
  139. public string para_post_baseform(dynamic obj)
  140. {
  141. var parapost = Convert.ToString(obj.parapost);
  142. Product p = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(Convert.ToString(obj.formdata));
  143. return "parapost:"+parapost+ " p.Name:" + p.Name + " p.Price:" + p.Price;
  144. }
  145.  
  146. /// <summary>
  147. /// post 数组参数
  148. /// </summary>
  149. /// <param name="ids"></param>
  150. /// <returns></returns>
  151. [Route("api/Product/post_array")]
  152. [HttpPost]
  153. public string post_array(string[] ids)
  154. {
  155. string result = "";
  156. for (int i = ; i < ids.Length; i++) {
  157. result += " :" + ids[i];
  158. }
  159. return result;
  160. }
  161.  
  162. /// <summary>
  163. /// post 实体数组参数
  164. /// </summary>
  165. /// <param name="ids"></param>
  166. /// <returns></returns>
  167. [Route("api/Product/post_ojblist")]
  168. [HttpPost]
  169. public string post_ojblist(List<Product> plist)
  170. {
  171. string result = "";
  172. for (int i = ; i < plist.Count; i++)
  173. {
  174. result +=" <br/>||"+i+ ":name:" + plist[i].Name+" price:"+plist[i].Price;
  175. }
  176. return result;
  177. }
  178. }
  179. }

webapi

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace wxweb.Areas.API.Models
  7. {
  8. public class Product
  9. {
  10. public int Id { get; set; }
  11. public string Name { get; set; }
  12. public string Category { get; set; }
  13. public decimal Price { get; set; }
  14. }
  15. }

实体类

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Product App</title>
  5. </head>
  6. <body>
  7.  
  8. <div>
  9. <h2>All Products from get Method</h2>
  10. <ul id="products" />
  11. </div>
  12. <div>
  13. <h2>Search by ID from get Method</h2>
  14. <input type="text" id="prodId" size="5" />
  15. <input type="button" value="Search" onclick="find();" />
  16. <p id="product" />
  17. </div>
  18.  
  19. <div style="border-top:1px solid #ccc;">
  20. <h2>All Products from post Method</h2>
  21. <ul id="products_post" />
  22. </div>
  23. <div>
  24. <h2>Search by ID from post Method</h2>
  25. <input type="text" id="prodId_post" size="5" />
  26. <input type="button" value="Search" onclick="findpost();" />
  27. <p id="product_post" />
  28. </div>
  29.  
  30. <div style="border-top:2px solid #ccc;">
  31. <h3>多参数传参</h3>
  32. id:<input type="text" id="get_id" name="=get_id" /><br />
  33. name:<input type="text" id="get_name" name="=get_name" /><br />
  34. <input type="button" value="get传参" onclick="para_get_base()" /><br />
  35. <span>get方式api调用结果</span> <span id="get_result"></span><br />
  36. <input type="button" value="post传参" onclick="para_post_base()" /><br />
  37. <span>post方式api调用结果</span><span id="get_result_post"></span><br />
  38. </div>
  39.  
  40. <div style="border-top:2px solid #ccc;">
  41. <h3>实体传参</h3>
  42. <form id="form1">
  43. base:<input type="text" id="parapost"><br />
  44. Id:<input type="text" id="Id" name="Id" /><br />
  45. Name:<input type="text" id="Name" name="Name" /><br />
  46. Category:<input type="text" id="Category" name="Category" /><br />
  47. Price:<input type="number" id="Price" name="Price" /><br />
  48. </form>
  49.  
  50. <input type="button" value="get form传参" onclick="para_get_form()" /><br />
  51. <span>get方式api调用结果</span> <span id="get_result_form"></span><br />
  52. <input type="button" value="post form传参" onclick="para_post_form()" /><br />
  53. <span>post方式api调用结果</span> <span id="get_result_form_post"></span><br />
  54. <input type="button" value="post base+form传参" onclick="para_post_baseform()" /><br />
  55. <span>post base+form方式api调用结果</span> <span id="get_result_baseform_post"></span><br />
  56. </div>
  57.  
  58. <div style="border-top:2px solid #ccc;">
  59. <h3>数组传参</h3>
  60.  
  61. <input type="button" value="post数组传参" onclick="post_array()" /><br />
  62. <span>post方式api调用结果</span><span id="post_array"></span><br />
  63. </div>
  64.  
  65. <div style="border-top:2px solid #ccc;">
  66. <h3>实体集合</h3>
  67.  
  68. <input type="button" value="post实体集合" onclick="post_ojblist()" /><br />
  69. <span>post方式api调用结果</span><span id="post_ojblist"></span><br />
  70. </div>
  71.  
  72. <div style="margin-bottom:100px;"></div>
  73. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
  74. <script src="../Scripts/jquery.serializeObject.js"></script>
  75. <script type="text/javascript">
  76.  
  77. $(document).ready(function () {
  78.  
  79. var uri = '/api/Product';
  80. var para = '';
  81. //get方式获取
  82. $.get(uri, para,
  83. function (data) {
  84. $.each(data, function (key, item) {
  85. $('<li>', { text: formatItem(item) }).appendTo($('#products'));
  86. });
  87. })
  88. var uripost = '/api/Product/GetProducts';
  89. //post方式获取
  90. $.post(uripost, para,
  91. function (data) {
  92. $.each(data, function (key, item) {
  93. $('<li>', { text: formatItem(item) }).appendTo($('#products_post'));
  94. });
  95. })
  96.  
  97. });
  98.  
  99. function formatItem(item) {
  100. return item.Name + ': $' + item.Price;
  101. }
  102. //使用get方式 单参数查询
  103. function find() {
  104. var uri = '/api/Product/GetProductById';
  105. var id = $('#prodId').val();
  106. var para = { 'id': id };
  107. //var uri = '';
  108. $.get(uri, para,
  109. function (data) {
  110. $('#product').text(formatItem(data));
  111. })
  112. }
  113. //使用post方式 单参数查询
  114. function findpost() {
  115. var uri = '/api/Product/GetProduct';
  116. var id = $('#prodId_post').val();
  117. var para = { '': id };//注意传递参数的参数名称,填''
  118. //var uri = '';
  119. $.post(uri, para,
  120. function (data) {
  121. $('#product_post').text(formatItem(data));
  122. })
  123. }
  124.  
  125. //get方式多参数传参
  126. function para_get_base() {
  127. var uri = '/api/Product';
  128. var id = $('#get_id').val();
  129. var name = $('#get_name').val();
  130. var para = { id: id, name: name };
  131. $.ajax({
  132. url: uri,
  133. type: 'get',
  134. async: true,
  135. data: para,
  136. dataType: 'text',
  137. success: function (r) {
  138. $('#get_result').html(r);
  139. }, error: function (r) {
  140. console.log(r);
  141. }
  142. });
  143. }
  144.  
  145. //post方式多参数传参
  146. function para_post_base() {
  147. var uri = '/api/Product/para_post_base';
  148. var id = $('#get_id').val();
  149. var name = $('#get_name').val();
  150. var para = { id: id, name: name };
  151. para = JSON.stringify(para);//js对象转化为字符传递
  152. $.ajax({
  153. url: uri,
  154. type: 'post',
  155. async: true,
  156. data: para,
  157. dataType: 'text',
  158. contentType: "application/json",
  159. success: function (r) {
  160. $('#get_result_post').html(r);
  161. }, error: function (r) {
  162. console.log(r);
  163. }
  164. });
  165. }
  166.  
  167. //get方式传递form参数
  168. function para_get_form() {
  169. var uri = '/api/Product/para_get_form';
  170.  
  171. //参数方式1,借助serializeObject 将form序列化为json对象
  172. var para = $('#form1').serializeObject();
  173. //参数方式2,依次写出每一个参数值
  174. var para2 = { Id: $('#Id').val(), Name: $('#Name').val(), Category: $('#Category').val(), Price: $('#Price').val() };//参数方式2,
  175. $.ajax({
  176. url: uri,
  177. type: 'get',
  178. async: true,
  179. data: para,
  180. contentType: "application/json",
  181. dataType: 'text',
  182. success: function (r) {
  183. $('#get_result_form').html(r);
  184. }, error: function (r) {
  185. alert(r);
  186. }
  187. });
  188. }
  189.  
  190. //post方式传递form参数
  191. function para_post_form() {
  192. var uri = '/api/Product/para_post_form';
  193.  
  194. //参数方式1,借助serializeObject 将form序列化为json对象
  195. var para = $('#form1').serializeObject();
  196. //参数方式2,依次写出每一个参数值
  197. var para2 = { Id: $('#Id').val(), Name: $('#Name').val(), Category: $('#Category').val(), Price: $('#Price').val() };//参数方式2,
  198. $.ajax({
  199. url: uri,
  200. type: 'post',
  201. async: true,
  202. data: para,
  203. dataType: 'text',
  204. success: function (r) {
  205. $('#get_result_form_post').html(r);
  206. }, error: function (r) {
  207. alert(r);
  208. }
  209. });
  210. }
  211.  
  212. //post方式传递form参数
  213. function para_post_baseform() {
  214. var uri = '/api/Product/para_post_baseform';
  215.  
  216. //参数方式1,借助serializeObject 将form序列化为json对象
  217. var formdata = $('#form1').serializeObject();
  218. var para = JSON.stringify({ parapost: $('#parapost').val(), formdata: formdata })
  219.  
  220. $.ajax({
  221. url: uri,
  222. type: 'post',
  223. async: true,
  224. data: para,
  225. dataType: 'text',
  226. contentType: "application/json",
  227. success: function (r) {
  228. $('#get_result_baseform_post').html(r);
  229. }, error: function (r) {
  230. alert(r);
  231. }
  232. });
  233. }
  234.  
  235. function post_array() {
  236. var arr = ["1", "2", "3", "4"];
  237. var para = JSON.stringify(arr);
  238. var uri = '/api/Product/post_array';
  239. $.ajax({
  240. type: "post",
  241. url: uri,
  242. contentType: 'application/json',
  243. data: para,
  244. dataType: 'text',
  245. success: function (data) {
  246. $('#post_array').html(data);
  247. },
  248. error: function (data) {
  249. console.log(data);
  250. }
  251.  
  252. });
  253. }
  254.  
  255. function post_ojblist() {
  256. var arr = [
  257. { Id: 1, Name: "Jim", Category: "001", Price:10 },
  258. { Id: 2, Name: "jack", Category: "002", Price: 20 },
  259. { Id: 3, Name: "tom", Category: "003", Price: 30 }
  260. ];
  261. var para = JSON.stringify(arr);
  262. var uri = '/api/Product/post_ojblist';
  263. $.ajax({
  264. type: "post",
  265. url: uri,
  266. contentType: 'application/json',
  267. data: para,
  268. dataType: 'text',
  269. success: function (data, status) {
  270. if (status) {
  271. $('#post_ojblist').html(data);
  272. } else {
  273. console.log(data);
  274. }
  275. }, error: function (data) {
  276. console.log(data);
  277. }
  278.  
  279. });
  280. }
  281. </script>
  282. </body>
  283. </html>

html文件

  1. //
  2. // Use internal $.serializeArray to get list of form elements which is
  3. // consistent with $.serialize
  4. //
  5. // From version 2.0.0, $.serializeObject will stop converting [name] values
  6. // to camelCase format. This is *consistent* with other serialize methods:
  7. //
  8. // - $.serialize
  9. // - $.serializeArray
  10. //
  11. // If you require camel casing, you can either download version 1.0.4 or map
  12. // them yourself.
  13. //
  14.  
  15. (function($){
  16. $.fn.serializeObject = function () {
  17. "use strict";
  18.  
  19. var result = {};
  20. var extend = function (i, element) {
  21. var node = result[element.name];
  22.  
  23. // If node with same name exists already, need to convert it to an array as it
  24. // is a multi-value field (i.e., checkboxes)
  25.  
  26. if ('undefined' !== typeof node && node !== null) {
  27. if ($.isArray(node)) {
  28. node.push(element.value);
  29. } else {
  30. result[element.name] = [node, element.value];
  31. }
  32. } else {
  33. result[element.name] = element.value;
  34. }
  35. };
  36.  
  37. $.each(this.serializeArray(), extend);
  38. return result;
  39. };
  40. })(jQuery);

jquery.serializeObject.js

2. get 请求

1>:实体作为参数调用时,前台的实体参数无法直接传递到后台,需在webapi中对实体参数加上[FromUri]特性说明。

这是因为通过get方式传递的参数都是通过Request URL传递到后台的,则FromUri 给api说明 参数是从Request URL中获取到的。

如下:

  1. /// <summary>
  2. /// get form传参
  3. /// </summary>
  4. /// <param name="p"></param>
  5. /// <returns></returns>
  6. [Route("api/Product/para_get_form")]
  7. [HttpGet]
  8. public string para_get_form([FromUri]Product p)
  9. {
  10. return "p.Name:" + p.Name + " p.Price:" + p.Price;
  11. }

get form传参 api

  1. //get方式传递form参数
  2. function para_get_form() {
  3. var uri = '/api/Product/para_get_form';
  4.  
  5. //参数方式1,借助serializeObject 将form序列化为json对象
  6. var para = $('#form1').serializeObject();
  7. //参数方式2,依次写出每一个参数值
  8. var para2 = { Id: $('#Id').val(), Name: $('#Name').val(), Category: $('#Category').val(), Price: $('#Price').val() };//参数方式2,
  9. $.ajax({
  10. url: uri,
  11. type: 'get',
  12. async: true,
  13. data: para,
  14. contentType: "application/json",
  15. dataType: 'text',
  16. success: function (r) {
  17. $('#get_result_form').html(r);
  18. }, error: function (r) {
  19. alert(r);
  20. }
  21. });
  22. }

get form传参 js调用

  2> 如果不想通过[FromUri]特性说明的方式进行传递实体,也可以使用把实体先序列化在反序列化的方式传递。

  1. $.ajax({
  2. type: "get",
  3. url: "http://localhost:27221/api/Charging/GetByModel",
  4. contentType: "application/json",
  5. data: { strQuery: JSON.stringify({ ID: "1", NAME: "Jim", CREATETIME: "1988-09-11" }) },
  6. success: function (data, status) {
  7. if (status == "success") {
  8. $("#div_test").html(data);
  9. }
  10. }
  11. });
  12.  
  13. [HttpGet]
  14. public string GetByModel(string strQuery)
  15. {
  16. TB_CHARGING oData = Newtonsoft.Json.JsonConvert.DeserializeObject<TB_CHARGING>(strQuery);
  17. return "ChargingData" + oData.ID;
  18. }

get 实体传参 通过序列化的方式

get 方式总结

(1)Get参数传递的本质是url字符串拼接;

(2)url字符串长度受限制;

(3)Get参数传递在Http请求头部传递,而不支持Request-Body传递;

(4)Get类型的方法支持参数为基本类型,不支持实体类型;

(5)Get类型的方法命名,应尽量采用“Get+方法名”的命名方式,且习惯性地在方法前加上[HttpGet特性];

(6)实参与形参的匹配,遵循路由规则;

(7)Get对应DB的Select操作,从这一点来理解,就知道为什么Http不支持实体对象传递的合理性了,因为一般情况,我们都是通过简单的字段查询信息(对应基本类型),

如ID号,用户名等,而不会通过一个实体查询数据;

3 post 请求

  1> post 单参数请求

  我们一般的通过url取参数的机制是键值对,即某一个key等于某一个value,而这里的FromBody和我们一般通过url取参数的机制则不同,它的机制是=value,没有key的概念,并且如果你写了key(比如你的ajax参数写的{id:"1"}),后台反而得到的id等于null。

  1. //使用post方式 单参数查询
  2. function findpost() {
  3. var uri = '/api/Product/GetProduct';
  4. var id = $('#prodId_post').val();
  5. var para = { '': id };//注意传递参数的参数名称,填''
  6. //var uri = '';
  7. $.post(uri, para,
  8. function (data) {
  9. $('#product_post').text(formatItem(data));
  10. })
  11. }
  12.  
  13. /// <summary>
  14. /// post 单个参数传参
  15. /// </summary>
  16. /// <param name="id"></param>
  17. /// <returns></returns>
  18. [Route("api/Product/GetProduct")]
  19. [HttpPost]
  20. public IHttpActionResult GetProduct([FromBody]string id)
  21. {
  22. var product = products_post.FirstOrDefault((p) => p.Id ==Convert.ToInt32( id));
  23. if (product == null)
  24. {
  25. return NotFound();
  26. }
  27. return Ok(product);
  28. }

post 单参数请求

  2> 多参数请求

post方式是不支持多参数方式请求的,为了传递多参数我们可以采用其他的方式。

比如,在后台建立对象实体,然后将整个实体作为对象传递,

  1. //post方式传递form参数
  2. function para_post_form() {
  3. var uri = '/api/Product/para_post_form';
  4.  
  5. //参数方式1,借助serializeObject 将form序列化为json对象
  6. var para = $('#form1').serializeObject();
  7. //参数方式2,依次写出每一个参数值
  8. var para2 = { Id: $('#Id').val(), Name: $('#Name').val(), Category: $('#Category').val(), Price: $('#Price').val() };//参数方式2,
  9. $.ajax({
  10. url: uri,
  11. type: 'post',
  12. async: true,
  13. data: para,
  14. dataType: 'text',
  15. success: function (r) {
  16. $('#get_result_form_post').html(r);
  17. }, error: function (r) {
  18. alert(r);
  19. }
  20. });
  21. }
  22.  
  23. /// <summary>
  24. /// post form传参
  25. /// </summary>
  26. /// <param name="p"></param>
  27. /// <returns></returns>
  28. [Route("api/Product/para_post_form")]
  29. [HttpPost]
  30. public string para_post_form(Product p)
  31. {
  32. return "p.Name:" + p.Name + " p.Price:" + p.Price;
  33. }

post 通过对象实体传递多参数

  html页面中如果使用form序列化的方式,可以调用serializeObject.js的方法得到json对象。

比如,使用dynamic参数,动态的获取所需要的参数。

  1. //post方式多参数传参
  2. function para_post_base() {
  3. var uri = '/api/Product/para_post_base';
  4. var id = $('#get_id').val();
  5. var name = $('#get_name').val();
  6. var para = { id: id, name: name };
  7. para = JSON.stringify(para);//js对象转化为字符传递
  8. $.ajax({
  9. url: uri,
  10. type: 'post',
  11. async: true,
  12. data: para,
  13. dataType: 'text',
  14. contentType: "application/json",
  15. success: function (r) {
  16. $('#get_result_post').html(r);
  17. }, error: function (r) {
  18. console.log(r);
  19. }
  20. });
  21. }
  22.  
  23. /// <summary>
  24. /// post 多参数传参
  25. /// </summary>
  26. /// <param name="obj"></param>
  27. /// <returns></returns>
  28. [Route("api/Product/para_post_base")]
  29. [HttpPost]
  30. public string para_post_base(dynamic obj)
  31. {
  32. string id = obj["id"].ToString();
  33. string name = obj["name"].ToString();
  34. return "id:" + id + " name:" + name;
  35. }

post 通过dynamic 传递多参数

post 方式总结

(1)Post参数传递本身是在Request-Body内传递,而Get参数传递本质是url拼接;

(2)Post参数传递不是key/value形式,而Get参数是key/value形式;

(3)Post传递参数时,无论是单个参数还是对象,均借助[FromBody]特性(当然,某些情况去掉[FromBody]特性也可把值传递进去,但为了规范化,尽量加上该特性);

(4)Post没长度限制,而Get有长度限制(一般为1024b);

(5)Post相对Get,较安全;

(6)Post操作相当于DB的Insert操作;

4 总结

1.虽然HTTP请求方法有20多种,常用的大致为4种,即Get,Post,Put,Delete(当然,像Trace,Head等也常用);

2.Get,Post,Put,Delete分别对应DB的Select,Insert,Update和Delete操作;

3.WebApi参数类型,大致分为基本数据类类型和对象数据类型(当然你也可以理解为抽象数据类型);

4.研究WebApi参数传递,只需研究Get和Post即可,因为其他http方法参数传递基本都是有这两种组合而成(如Put有Get和Post组合而成),或者相似(如Delete与Get相似);

5.对于控制器方法,尽量参照规范格式写,如在相应控制器方法上加上对应的htt请求(Get对应[HttpGet],Post对应[HttpPost]),方法名尽量采用“Http请类型+方法名”格式(如Get请求,建议采用Get+MethodName;Post请求对应Post+MethodName);

6.WebApi参数请求,大致分为两大类型,即Request-url和Request-body;

引用:

https://www.cnblogs.com/landeanfen/p/5337072.html#_label3

http://www.cnblogs.com/wangjiming/p/8378108.html

webapi-2 接口参数的更多相关文章

  1. WebApi 接口参数详解

    WebApi 接口参数不再困惑:传参详解   阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4.“怪异”的get请求 二.post请求 1.基础类型参数 2.实体作为 ...

  2. [置顶] webapi token、参数签名是如何生成的

    一个问题 在这里我想问大家一句,如果你向一个刚刚接触.net web后端程序开发的同学(别人刚刚也就学了webform的request,response,会提交表单的这种刚接触不久的同学),你怎么去解 ...

  3. webapi token、参数签名是如何生成的(转载)

    API接口保障安全性原则:1.有调用者身份2.请求的唯一性3.请求的参数不能被篡改4.请求的有效时间 在刚接触接口开发时,可能脑子里压根就没有这个接口调用安全性的原则,但常识性的经验告诉我们,每一个请 ...

  4. 如何写出安全的API接口?接口参数加密签名设计思路

    开发中经常用到接口,尤其是在面向服务的soa架构中,数据交互全是用的接口. 几年以前我认为,我写个接口,不向任何人告知我的接口地址,我的接口就是安全的,现在回想真是too young,too simp ...

  5. Asp.Net Mvc4 Webapi Request获取参数

    最近用mvc4中的WEBAPI,发现接收参数不是很方便,跟传统的request.querystring和request.form有很大区别,在网上搜了一大圈,各种方案都有,但不是太详细,于是跟踪Act ...

  6. WebApi 方法的参数类型总结。

    1:[HttpGet]  ①:get方法之无参数. [HttpGet] public IHttpActionResult GetStudentInfor() { List<StudentMode ...

  7. Asp.Net WebAPI配置接口返回数据类型为Json格式

    Asp.Net WebAPI配置接口返回数据类型为Json格式   一.默认情况下WebApi 对于没有指定请求数据类型类型的请求,返回数据类型为Xml格式 例如:从浏览器直接输入地址,或者默认的XM ...

  8. c# WebApi之接口返回类型详解

    c# WebApi之接口返回类型详解 https://blog.csdn.net/lwpoor123/article/details/78644998

  9. mybatis 多个接口参数的注解使用方式(@Param)

    目录 1 简介 1.1 单参数 1.2 多参数 2 多个接口参数的两种使用方式 2.1 Map 方法(不推荐) 2.1.1 创建接口方法 2.1.2 配置对应的SQL 2.1.3 调用 2.2 @Pa ...

  10. ASP.NET WebApi服务接口如何防止重复请求实现HTTP幂等性

    一.背景描述与课程介绍 明人不说暗话,跟着阿笨一起玩WebApi.在我们平时开发项目中可能会出现下面这些情况; 1).由于用户误操作,多次点击网页表单提交按钮.由于网速等原因造成页面卡顿,用户重复刷新 ...

随机推荐

  1. Gevent-自动挡切换

    Gevent: Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程. G ...

  2. 【以前的空间】bzoj 1052 [HAOI2007]覆盖问题

    这道题的思路挺简单的……就是可以证明如果要覆盖一个区域内的点,那么一定有一个正方形在这“区域内的点所围成的最大矩形的四个角中的一个”(不要吐槽很多的“的”……),对于长度r是否可以覆盖整个区域内的点, ...

  3. POJ3177:Redundant Paths——题解

    http://poj.org/problem?id=3177 明显要求桥的一道题. (因为有桥就说明只能从那一条路走,换句话说就是只有一种方法) 求完桥后按照结论(加几条边成双连通图的结论,不会请ba ...

  4. BZOJ1068:[SCOI2007]压缩——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 Description 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复 ...

  5. [bzoj] 1030 文本生成器 || AC自动机+dp

    原题 给出n个字符串,求随机生成一个m长度的字符串,有多少个是可辨识的(即出现了n个字符串中的任意字符串) 正难则反 求有多少个不可辨识的,26^m-不可辨识即为答案 f[i][j]表示填到第i个字符 ...

  6. 洛谷 U32911 道路维护 解题报告

    U32911 道路维护 题目背景 最近很多人投诉说C国的道路破损程度太大,以至于无法通行. C国的政府很重视这件事,但是最近财政有点紧,不可能将所有的道路都进行维护,所以他们决定按照下述方案进行维护. ...

  7. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

  8. redux的bindActionCreators

    bindActionCreators是redux的一个API,作用是将单个或多个ActionCreator转化为dispatch(action)的函数集合形式. 开发者不用再手动dispatch(ac ...

  9. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  10. NDK plugin来构建JNI项目(相对于手动构建)

    http://blog.csdn.net/codezjx/article/details/8879670 1.添加ndk环境支持 Android Tools -> Add Native Supp ...