[Web API] Client 指定請求及響應格式(xml/json)
Web API 支援的格式請參考
http://www./web-/overview/formats-and-model-binding
本篇則要演練xml/json
回應 Get
定義 Header:Content-Type 定義 QueryString
請求 Post
POST json 資料 POST xml 資料
定義 Header:Content-Type
:本來想用 postman 但是設定 Content-Type 一直無法成功,嘗試一段時間便改用 fiddler
指定 json:application/json
指定 xml:application/xml
[

]
觀察回傳格式
定義 QueryString
在 WebApiConfig.cs 的 Register 方法加上
在網址列加上 ?$format=xml,便可以指定回傳格式
參考:http://code.msdn.microsoft.com/Support-format-in-ASPNET-e3785b2a
POST json 資料
{
"ISBN":"111","AuthiorName":"空","BookName":"空","Category":"空","Price":1
}
回傳結果
POST Xml
<Product ns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPI_CRUD.Models">
<AuthiorName>周家安</AuthiorName>
<BookName>Windows Phone8開發技巧與案例精解</BookName>
<Category>.NET</Category>
<ISBN>9787111412304</ISBN>
<Price>69</Price>
</Product>
步驟如下:
本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/p/4923717.html,如需轉載請自行聯系原作者