首頁 > 後端開發 > C#.Net教程 > 操作 ASP.NET Web API 的實例教學

操作 ASP.NET Web API 的實例教學

零下一度
發布: 2017-06-23 15:12:04
原創
3253 人瀏覽過

 

概述

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類別。

操作 ASP.NET Web API 的實例教學
 1 namespace WebAPI.Models
# 2 {
 3 public class Users
 4     {
 5         public ) # 6  7         
public string UserName { 
#get; set; }" 8get; ##set; } #string UserEmail { 
get; #set; }
#10     }11 }

1
操作 ASP.NET Web API 的實例教學
將自動產生的ValueController修改成UsersController。

GET數據

使用HTTP的get方法請求獲取數據,整個Web API的請求處理基於MVC框架。

程式碼如下。

操作 ASP.NET Web API 的實例教學
 1 using System;
# 2 using System.Collections.Generic;
 3 using System.Linq;
##q;##q # 4 使用System.Net;
## 5 使用System.Net.Http;
 6 使用System.Web.Http;
# 6 使用System.Web.Http;
using WebAPI.Models;
 8 
# 9 命名空間 WebAPI.Controllers
##10 {#11   # 
10 {
#11   # 10 class UsersController : ApiController
12     {13          //
/// 使用者資料清單15         /// 
#摘要>#16         private 
readonly 清單 _userList = new 清單
17         {18           #", UserEmail = "Superman@cnblogs.com"},19              新使用者{UserID =
2, UserName    新使用者{UserID = 2, UserName    新使用者{ ##Spiderman", UserEmail = "Spiderman@cnblogs.com"},20             ##Name#新使用者{UserID =        ##Name#新用戶{
"Batman", UserEmail = "Batman@cnblogs.com"}21         };##22        }; #23         
//# GET api/Users

24         關於「」 Get()25         {
26             1##          / / GET api/Users/5
#30         public Users GetUserByID(int id)
#31         _userList.FirstOrDefault(users => users . UserID == id);
33             if (user == 
null)## #           throw new HttpResponseException (HttpStatusCode.NotFound);##36             }
37         
39 40 //
GET api/Users/?username=xx
41         
public IEnumerable; GetUserByName(string 使用者名稱)
42         {
43            arison. OrdinalIgnoreCase));44         }
45     }46 }




操作 ASP.NET Web API 的實例教學
###########

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.

操作 ASP.NET Web API 的實例教學
# 1
//POST api/Users/Users Entity Json 2
public Users Add([FromBody]Users users) 3 {
4  
if (users == null) 5                                                                                                                                                                                                                         
9   return users;
10 }



We still use Fiddler to simulate POST data.
Before the POST request, we first attach the code to the process and set a breakpoint at the Add method.
In Visual Studio 2012, the debug HOST program becomes IIS Express.
操作 ASP.NET Web API 的實例教學We use Ctrl+ALT+P to attach to its process.

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.

Conclusion

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中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板