ASP.NET中Web API的參數(shù)綁定
在這篇文章中,我們將學習WebAPI如何將HTTP請求數(shù)據(jù)綁定到一個操作方法的參數(shù)中。
操作方法在WebAPI控制器中可以有一個或多個不同類型的參數(shù)。它可以是基本數(shù)據(jù)類型或復雜類型。WebAPI根據(jù)URL的查詢字符串或請求主體中參數(shù)類型來綁定操作方法的參數(shù)。
如果參數(shù)類型是基本數(shù)據(jù)類型(int,double,string,DateTime,bool等),WebAPI默認將會從URL中獲取參數(shù)值(即通過querystring)獲取。
如果參數(shù)類型的復雜類型,WebAPI默認從請求主體中獲取參數(shù)值。
下表列出了Web API參數(shù)綁定的默認規(guī)則。
HTTP方法 | Query String | Request Body |
---|---|---|
GET | 簡單類型 | 不能從請求主體中獲取參數(shù)值 |
POST | 簡單類型 | 復雜類型 |
PUT | 簡單類型 | 復雜類型 |
PATCH | 簡單類型 | 復雜類型 |
DELETE | 簡單類型 | 不能從請求主體中獲取參數(shù)值 |
我們先來看看創(chuàng)建帶MVC的Web API項目時自動生成的Values控制器是如何定義的。
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace WebAPIContainMVC.Controllers { public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } // GET api/values/5 public string Get(int id) { return "value"; } // POST api/values public void Post([FromBody]string value) { } // PUT api/values/5 public void Put(int id, [FromBody]string value) { } // DELETE api/values/5 public void Delete(int id) { } } }
從Values控制器的定義中,我們可以得出如下幾個結(jié)論:
(1)WebAPI常規(guī)的方法有四個:Get(),Post(),Put()和Delete()。
(2)四種方法的參數(shù)可以歸納為兩大類:URL傳遞(Request-url)和Body(Request-body)傳遞。
(3)可以將四種方法的參數(shù)傳遞歸為兩大類,而這兩大類又集中在Get和Post中體現(xiàn)(Put是Get和Post的組合,Delete和Get類型),所以說研究WebAPI的參數(shù)綁定,只需要研究Get和Post方法的參數(shù)傳遞即可,Get對應Request-url,Post對應Request-Body。
在本篇文章中,客戶端調(diào)用使用Fiddler工具進行測試。
一、Get
1、基本數(shù)據(jù)類型作為方法參數(shù)
1.1方法只含有一個參數(shù)(形參可以傳遞進來)
方法定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebAPIContainMVC.Entity; namespace WebAPIContainMVC.Controllers { public class StudentController : ApiController { [HttpGet] public string GetStudentById(int id) { string strResult=string.Empty; List<Student> list = new List<Student>(); Student stu = new Student() { StudentId=1, Name="Tom", Age=20, Sex="男" }; list.Add(stu); list.ForEach(p => { if (p.StudentId.Equals(id)) { strResult = "存在該學生信息"; } else { strResult = "對不起,找不到該學生信息"; } }); return strResult; } } }
客戶端調(diào)用
結(jié)果:
雙擊左側(cè)的進程,得到如下的結(jié)果
1.2、方法含有多個參數(shù)(形參可以傳遞進來)
方法定義如下:
public string GetStudentByIdAndName(int id,string name) { string strResult = string.Empty; List<Student> list = new List<Student>(); Student stu = new Student() { StudentId = 1, Name = "Tom", Age = 20, Sex = "男" }; list.Add(stu); list.ForEach(p => { if (p.StudentId.Equals(id)&&p.Name.Equals(name)) { strResult = "存在該學生信息"; } else { strResult = "對不起,找不到該學生信息"; } }); return strResult; }
URL地址:http://localhost:63512/api/student?id=1&&name=Tom
結(jié)果如下:
2、實體對象類型作為方法參數(shù)(形參不能傳遞進來)
方法定義如下:
[HttpGet] public string GetStudent(Student student) { string strResult = string.Empty; List<Student> list = new List<Student>(); Student stu = new Student() { StudentId = 1, Name = "Tom", Age = 20, Sex = "男" }; list.Add(stu); if (student != null) { list.ForEach(p => { if (p.StudentId.Equals(student.StudentId)) { strResult = "存在該學生信息"; } else { strResult = "對不起,找不到該學生信息"; } }); } else { strResult = "參數(shù)值為Null"; } return strResult; }
客戶端調(diào)用結(jié)果如下:
3、基本數(shù)據(jù)類型和實體對象混合作為方法參數(shù)(基本數(shù)據(jù)類型可以傳遞進來,實體對象不能傳遞進來)
4、總結(jié)
(1)查詢字符串參數(shù)名稱和操作方法參數(shù)名稱必須相同(不區(qū)分大小寫)。參數(shù)的先后順序可以不一致。
(2)Get()參數(shù)傳遞的本質(zhì)是URL字符串拼接,但是URL字符串的長度受限制。
(3)Get()的參數(shù)支持基本數(shù)據(jù)類型,不支持實體數(shù)據(jù)類型。
(4)Get()參數(shù)的傳遞是在Http請求的頭部傳遞,而不支持Request-Body傳遞。
(5)Get類型的方法命名,盡量采用“Get+方法名”的命名方式,在方法前面加上特性:[HttpGet]。
二、Post
1、Post參數(shù)傳遞是在Request-Body內(nèi)傳遞。
2、Post參數(shù)可以傳遞基本數(shù)據(jù)類型,也可以傳遞實體數(shù)據(jù)類型。
3、Post操作方法只能包含一個實體數(shù)據(jù)類型,因為只能允許一個參數(shù)讀取Request-Body中的數(shù)據(jù)。
4、Post傳遞參數(shù)時,無論是基本類型參數(shù)還是實體類型,均需要借助[FromBody]特性。(有些情況下不寫[FromBody]特性也可以把參數(shù)傳遞進來,但為了規(guī)范化,最好是加上該特性)。
5、Post類型的方法命名,盡量采用“Post+方法名”的命名方式,在方法前面加上特性:[HttpPost]。
三、FromURI與FromBody
在默認情況下,WebAPI是從查詢字符串中得到基本數(shù)據(jù)類型參數(shù)的值,從請求主體中得到復雜類型參數(shù)的值,如果想改變這種默認情況怎么辦?
可以使用[FromURI]屬性,是WebAPI從查詢字符串中獲取復雜類型參數(shù)的值,使用[FromBody]屬性可以使WebAPI從請求主體中獲取基本數(shù)據(jù)類型參數(shù)的值。
例如下面的Get方法
[HttpGet] public string GetStudentById([FromUri]Student stu) { }
Get方法中包括復雜的類型參數(shù),參數(shù)添加了[FromUri]屬性,WebAPI將試圖從查詢字符串中得到Student類型參數(shù)的值。
同樣,參考下面的Post方法:
[HttpPost] public string Post([FromBody]string name) { }
WebAPI將從請求主體中獲取基本類型參數(shù)的值,而不是從請求字符串中獲取。
到此這篇關(guān)于ASP.NET中Web API參數(shù)綁定的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式
這篇文章介紹了ASP.NET MVC中Controller控制器向View視圖傳值的幾種方式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03.net?程序通過?crontab?無法啟動手動執(zhí)行腳本啟動的方法
.net 網(wǎng)關(guān)程序需要設(shè)置定時重啟,按照日常操作先把正在運行的 PID kill 掉后,再執(zhí)行啟動服務。通過腳本無法啟動,試著把 .net 程序?qū)懗煞蘸螅l(fā)現(xiàn)是可以正常重啟的,本文給大家介紹下.net 程序通過 crontab 無法啟動手動執(zhí)行腳本啟動,感興趣的朋友一起看看吧2021-12-12asp.net SqlHelper數(shù)據(jù)訪問層的使用
如果不使用數(shù)據(jù)訪問層,那么你的代碼里會出現(xiàn)很多SqlConnection、SqlCommand、SqlDataReader、Open、 Close……這些類和方法,而且代碼量很大,讓你不勝其煩,而且代碼寫起來,其實都是體力活,沒有技術(shù)含量。2008-09-09ASP.NET?MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面
這篇文章介紹了ASP.NET?MVC實現(xiàn)登錄后跳轉(zhuǎn)到原界面的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09