REST(Representational State Transfer表述性狀態轉移)而產生的REST API的討論越來越多,微軟在ASP.NET中也加入了Web API的功能。
我們剛好看看Web API的使用,而且看目前的版本有沒有解決掉這個問題。
在安裝了Visual Studio 2012後,我們依序點選新建專案->已安裝範本->Web->ASP.NET MVC 4 Web Application新工程專案.
專案範本選擇Web API。
在Model裡面我們還是加入之前文章裡面使用的User類別。
GET數據
程式碼如下。
Constructed a user list and implemented three methods. Let's make the request below.
When using different browsers to request, you will find that the returned format is different.
Using Chrome request first, we found that the Content-Type in the HTTP Header is xml type.
We change the FireFox request again and find that the Content-Type is still xml type.
We use IE to request again and find that this is the case.
#Open the saved file and we find that the requested data is in JSON format.
The reason for this difference is that the Content-Type in the Request Header sent by different browsers is inconsistent.
We can use Fiddler to verify it.
Content-Type:text/json
##Content-Type:text/xml POST data Implement a function added by User, the accepted type is User entity, and our POST data is the corresponding JSON data , to see if the problems encountered by dudu in the Beta version have been solved.Use Fiddler to simulate POST.
Note that the Content-Type in the Request Header is text/json, and the json content of the POST is:
1 {"UserID":4,"UserName": "Parry","UserEmail":Parry@cnblogs.com}After clicking Execute, it jumps to the breakpoint we set earlier. Let’s take a look at the submitted data.
The problems dudu encountered in Beta have been solved.
The ASP.NET framework has developed along the way, and its functions are indeed becoming more and more powerful and convenient. I hope we can abandon the language debate and return to pure technical discussions. Everyone says that Microsoft's technology changes too fast. What is the nature of the change? Is it good to remain unchanged?
In the second part, we will take a look at some security verification issues in Web API.
If there are any errors, please point them out and discuss them.
If you like it, giving it a recommendation is the best affirmation for the article. :)
以上是操作 ASP.NET Web API 的實例教學的詳細內容。更多資訊請關注PHP中文網其他相關文章!