WebAPI 2參數綁定方法

簡單類型參數

Example 1: Sending a simple parameter in the Url

[RoutePrefix("api/values")]
public class ValuesController : ApiController
{// http://localhost:49407/api/values/example1?id=2[Route("example1")][HttpGet]public string Get(int id){return "value";}
}

?

Example 2: Sending simple parameters in the Url

// http://localhost:49407/api/values/example2?id1=1&id2=2&id3=3
[Route("example2")]
[HttpGet]
public string GetWith3Parameters(int id1, long id2, double id3)
{return "value";
}

?

Example 3: Sending simple parameters using attribute routing

// http://localhost:49407/api/values/example3/2/3/4
[Route("example3/{id1}/{id2}/{id3}")]
[HttpGet]
public string GetWith3ParametersAttributeRouting(int id1, long id2, double id3)
{return "value";
}

?

Example 4: Sending an object in the Url

// http://localhost:49407/api/values/example4?id1=1&id2=2&id3=3
[Route("example4")]
[HttpGet]
public string GetWithUri([FromUri] ParamsObject paramsObject)
{return "value:" + paramsObject.Id1;
}
?

Example 5: Sending an object in the Request body

[Route("example5")]
[HttpPost]
public string GetWithBody([FromBody] ParamsObject paramsObject)
{return "value:" + paramsObject.Id1;
}

注意 [FromBody] 只能用一次,多于一次將不能正常工作

Calling the method using Urlencoded in the body:

User-Agent: Fiddler
Host: localhost:49407
Content-Length: 32
Content-Type: application/x-www-form-urlencodedid1=1&id2=2&id3=3

webapiparams_01

Calling the method using Json in the body:

User-Agent: Fiddler
Host: localhost:49407
Content-Length: 32
Content-Type: application/json{ "Id1" : 2, "Id2": 2, "Id3": 3}
webapiparams_02

?

Calling the method using XML in the body

This requires extra code in the Global.asax

protected void Application_Start()
{var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;xml.UseXmlSerializer = true;
The client request is as follows:User-Agent: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-Length: 65<ParamsObject><Id1>7</Id1><Id2>8</Id2><Id3>9</Id3></ParamsObject>

?

webapiparams_03

?

?

數組和列表(Array,List)

Example 6: Sending a simple list in the Url

// http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9
[Route("example6")]
[HttpGet]
public string GetListFromUri([FromUri] List<int> paramsObject)
{if (paramsObject != null){return "recieved a list with length:" + paramsObject.Count;}return "NOTHING RECIEVED...";
}

Example 7: Sending an object list in the Body

// http://localhost:49407/api/values/example8
[Route("example8")]
[HttpPost]
public string GetListFromBody([FromBody] List<ParamsObject> paramsList)
{if (paramsList != null){return "recieved a list with length:" + paramsList.Count;}return "NOTHING RECIEVED...";
}

?

Calling with Json:

User-Agent: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-Length: 91[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]

webapiparams_05_json

Calling with XML:

User-Agent: Fiddler
Content-Type: application/xml
Host: localhost:49407
Content-Length: 258<ArrayOfParamsObject>
<ParamsObject><Id1>3</Id1><Id2>76</Id2><Id3>19</Id3></ParamsObject>
<ParamsObject><Id1>56</Id1><Id2>87</Id2><Id3>94</Id3></ParamsObject>
<ParamsObject><Id1>976</Id1><Id2>345</Id2><Id3>7554</Id3></ParamsObject>
</ArrayOfParamsObject>

?

webapiparams_04_xml

Example 8: Sending object lists in the Body

[Route("example8")]
[HttpPost]
public string GetListsFromBody([FromBody] List<List<ParamsObject>> paramsList)
{if (paramsList != null){return "recieved a list with length:" + paramsList.Count;}return "NOTHING RECIEVED...";
}

This is a little bit different to the previous examples. The body can only send one single object to Web API. Because of this, the lists of objects are wrapped in a list or a parent object.

POST http://localhost:49407/api/values/example8 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:49407
Content-Length: 185[[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}],[{"Id1":3,"Id2":76,"Id3":19},{"Id1":56,"Id2":87,"Id3":94},{"Id1":976,"Id2":345,"Id3":7554}]
]

?

?

?

?

自定義參數

What if the default parameter binding is not enough? Then you can use the ModelBinder class to change your parameters and create your own parameter formats. You could also use ActionFilters for this. Many blogs exist which already explains how to use the ModelBinder class. See the links underneath.

?

文件和二進制

Files or binaries can also be sent to Web API methods. The article demonstrates how to do this.

?

參考

http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/CustomParameterBinding/

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/

http://www.roelvanlisdonk.nl/?p=3505

http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-a-asp-net-web-api-rest-service

http://stackoverflow.com/questions/14628576/passing-an-json-array-to-mvc-web-api-via-get

轉載于:https://www.cnblogs.com/HQFZ/p/5871035.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395076.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395076.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395076.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

推薦幾個自己經常去的一些博客和網站

唐巧的技術博客objc中國Ray WenderlichCocoaDocs.orgNSHipsterLukes HomepageCocoabit | 做自己喜歡的事情轉載于:https://www.cnblogs.com/faceup/p/10423259.html

創建hugo博客_Hugo + Firebase:如何在幾分鐘內免費創建自己的靜態網站

創建hugo博客by Aravind Putrevu通過Aravind Putrevu Hugo Firebase&#xff1a;如何在幾分鐘內免費創建自己的靜態網站 (Hugo Firebase: How to create your own static website for free in minutes) Ever thought of having your own website for putting up your projec…

探測與響應是各企業機構在2017年的首要安全事務

作者系&#xff1a;Gartner首席研究分析師 Sid Deshpande &Gartner研究總監 Lawrence Pingree 2017年&#xff0c;各個企業正在改變其安全支出戰略&#xff0c;從僅注重防御轉而更加關心探測和響應程度。2017年的全球信息安全支出預計將達到900億美元&#xff0c;相較2016年…

java怎么引入html文件路徑_如何在public_html中讀取文件但在域外?使用相對路徑...

我正在嘗試從我的(附加組件)域目錄之外的目錄中讀取文件 . 這是我的目錄結構&#xff1a;public_html /domain /file_read.phpfile_write.phpsensitive /file.dat雖然我能夠使用“../sensitive/file.dat”寫入敏感&#xff0c;但我無法使用相同的方法進行讀取 . 有什么想法嗎&a…

JS基本概念(3)

【5】操作符 &#xff08;1&#xff09;一元操作符&#xff1a;只能操作一個值的操作符 遞增、遞減操作符a --a 前置    a a-- 后置&#xff08;這四個操作符對任何值都適用&#xff0c;不能轉換成數字的轉換為NaN&#xff09; 一元加、一元減操作符&#xff0…

csv文件怎么轉成excel_Java讀寫excel,excel轉成json寫入磁盤文件

pom讀寫excel主要的dependency<dependency> <groupId>org.apache.poigroupId> <artifactId>poiartifactId> <version>3.16version> dependency> <dependency> <groupId>org.apache.poigroupId> …

如何用Ant Design Pro框架做項目省力

1、熟悉React所有語法&#xff0c;以及redux、redux-saga、dva、一類的庫的能力 2、靈活運用該框架提供的基礎UI組件&#xff0c;想方設法利用現有的UI組件進行組合&#xff0c;盡可能減少工作量 轉載于:https://www.cnblogs.com/ww01/p/10430553.html

通過在Chipotle用餐了解模板方法設計模式

by Sihui Huang黃思慧 通過在Chipotle用餐了解模板方法設計模式 (Understanding the Template Method design pattern by eating at Chipotle) Object-Oriented Design Patterns in Life— gain an intuitive understanding of OO design patterns by linking them with real-…

Coriant助力Aureon部署100Gbps光纖網絡

根據相關消息顯示&#xff0c;光傳輸設備廠商Coriant日前表示已經向網絡傳輸和業務通信服務供應商Aureon Technology提供了7100納米分組光傳輸平臺&#xff0c;幫助其進行100Gbps光纖網絡的拓展。 該服務供應商&#xff08;Aureon&#xff09;將利用該分組光傳輸系統&#xff0…

python class tynu()_Visual Studio Express | Teraz Visual Studio Community

Program Visual Studio 2019 jest teraz dost?pnyDostosowany instalatorTwrz aplikacje w technologiach WPF, WinForms, platformy uniwersaln? systemu Windows, Win32, Android, iOS i innych — wszystko to za pomoc? jednego ?rodowiska IDE zapewniaj?cego wszyst…

css樣式中如何設置中文字體?

代碼如下: .selector{font-family: SimHei,"微軟雅黑",sans-serif;} 注意&#xff1a;加上中文名“微軟雅黑”是為了兼容opera瀏覽器&#xff0c;中文字體名必須加上引號&#xff08;單引號雙引號都可以&#xff09;。 MicrosoftJhengHei為微軟正黑體&#xff0c;STH…

前端做CRM管理系統是做什么_代辦行業的CRM客戶關系管理系統應該是什么樣子的?...

隨著互聯網的深耕細化&#xff0c;很多企業也在不斷優化自己的辦公方式&#xff0c;以優化企業的辦公流程&#xff0c;提高企業的辦事效率。因此實現辦公自動化&#xff0c;或者說實現數字化辦公就需要逐漸提上日程。今天給大家講講可以幫助代辦行業實現辦公自動化的產品&#…

(譯) JSON-RPC 2.0 規范(中文版)

http://wiki.geekdream.com/Specification/json-rpc_2.0.html 起源時間: 2010-03-26(基于2009-05-24版本) 更新: 2013-01-04 作者: JSON-RPC工作組< json-rpcgooglegroups.com > 原文鏈接: http://www.jsonrpc.org/specification翻譯: leozvc < xxfs91gmail.com >…

ios pusher使用_如何使用JavaScript和Pusher實時更新用戶狀態

ios pusher使用by Rahat Khanna通過拉哈特漢娜 如何使用JavaScript和Pusher實時更新用戶狀態 (How to update a User’s Status in realtime using JavaScript and Pusher) “Hey, what’s up?” is not a phrase we need to ask someone these days. These days knowing wha…

python + pyqt5 UI和信號槽分離方法

初級菜鳥&#xff0c;知識點記錄。 每次重新生成UI.py文件的時候&#xff0c;里面的按鈕方法都會被清除&#xff0c;想一個方法可以把按鈕響應方法放到外面&#xff0c;利于維護。 新建一個按鈕文件并繼承UI代碼&#xff0c;把信號槽及按鈕響應方法寫在按鈕文件里面&#xff0c…

學習之路~sqh

推薦博客 Edison Chou&#xff1b;Vamei&#xff1b;算法?面試專題 - 簡書&#xff1b;xingoo - 博客園&#xff1b;設計模式 極速理解設計模式系列【目錄索引】- Caleung&#xff1b;Net設計模式 - 靈動生活&#xff1b;宅男程序員給老婆的計算機課程系列&#xff1b;C設計模…

python format函數保留兩位小數_python format函數

在Python 3.0中&#xff0c;%操作符通過一個更強的格式化方法format()進行了增強。對str.format()的支持已經被反向移植到了Python 2.6在2.6中&#xff0c;8-bit字符串和Unicode字符串都有一個format()方法&#xff0c;這個方法會把字符串當作一個模版&#xff0c;通過傳入的參…

藍牙 sig base uuid_藍牙模塊采用陶瓷天線和PCB天線的區別

一、陶瓷天線陶瓷天線是一種適合于藍牙設備使用的小型化天線,又分為塊狀陶瓷天線和多層陶瓷天線。陶瓷天線占用空間很小、性能比較好&#xff1b; 帶寬窄&#xff0c;比較難做到多頻段&#xff1b;有效提高主板的整合度&#xff0c;并可降低天線對ID的限制&#xff1b;需要在主…

kubernetes系列12—二個特色的存儲卷configmap和secret

本文收錄在容器技術學習系列文章總目錄 1、configmap 1.1 認識configmap ConfigMap用于保存配置數據的鍵值對&#xff0c;可以用來保存單個屬性&#xff0c;也可以用來保存配置文件。ConfigMap跟secret很類似&#xff0c;但它可以更方便地處理不包含敏感信息的字符串。 1.2 創建…

華為完成拉美銅網寬帶G.fast技術部署測試

1/11/2016,英國大東通信巴拿馬分公司日前與華為公司發布消息稱&#xff0c;覆蓋拉丁美洲地區的最快銅纜寬帶服務系統成功完成初次測試。 作為巴拿馬地區領先的移動寬帶服務提供商&#xff0c;大東通信巴拿馬分公司也是當地最大的電信服務提供商&#xff0c;此次與華為合作在現有…