<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何使用 RestSharp 調(diào)用 WebAPI 接口

          共 3477字,需瀏覽 7分鐘

           ·

          2021-01-17 08:58

          REST 是由 Representational State Transfer 這三個(gè)單詞前綴合成,這種架構(gòu)風(fēng)格在前幾年特別流行,Restful API 的行為規(guī)范可以參考: https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design ?,通常 RESTful API 返回的格式為常見(jiàn)的 PlianText, JSON , XML 格式。

          RestSharp 是一個(gè)開源的 Http 客戶端類庫(kù),非常方便和 RESTful 格式的 Service 進(jìn)行交互,????的是,這個(gè)類庫(kù)封裝了 request 請(qǐng)求過(guò)程中復(fù)雜的細(xì)節(jié),而且 RestSharp 支持同步和異步兩種請(qǐng)求模式。

          這篇文章將會(huì)討論如何使用 RestSharp 去請(qǐng)求 Asp.NET Core 服務(wù)。

          實(shí)現(xiàn) DefaultController

          打開 DefaultController.cs 文件并用下面的代碼進(jìn)行替換。


          using?Microsoft.AspNetCore.Mvc;
          using?System.Collections.Generic;

          namespace?RESTAPIDemo.Controllers
          {
          ???[Route("api/[controller]")]
          ???[ApiController]
          ???public?class?DefaultController?:?ControllerBase
          ???{
          ???????private?readonly?Dictionary<int,?string>?authors?=?new?Dictionary<int,?string>();
          ???????public?DefaultController()
          ???????{
          ???????????authors.Add(1,?"Joydip?Kanjilal");
          ???????????authors.Add(2,?"Steve?Smith");
          ???????????authors.Add(3,?"Michele?Smith");
          ???????}
          ???????
          ???????[HttpGet]
          ???????public?List<string>?Get()
          ???????{
          ???????????List<string>?lstAuthors?=?new?List<string>();
          ???????????foreach?(KeyValuePair<int,string>?keyValuePair?in?authors)
          ???????????????lstAuthors.Add(keyValuePair.Value);
          ???????????return?lstAuthors;
          ???????}
          ???????
          ???????[HttpGet("{id}",?Name?=?"Get")]
          ???????public?string?Get(int?id)
          ???????{
          ???????????return?authors[id];
          ???????}
          ???????
          ???????[HttpPost]
          ???????public?void?Post([FromBody]?string?value)
          ???????{
          ???????????authors.Add(4,?value);
          ???????}
          ???????
          ???????[HttpPut("{id}")]
          ???????public?void?Put(int?id,?[FromBody]?string?value)
          ???????{
          ???????????authors[id]?=?value;
          ???????}

          ???????[HttpDelete("{id}")]
          ???????public?void?Delete(int?id)
          ???????{
          ???????????authors.Remove(id);
          ???????}
          ???}
          }

          參考上面的 DefaultController 類,可以發(fā)現(xiàn) Action 方法的名字對(duì)應(yīng)著 Http 動(dòng)詞的 GET,POST,PUT 和 DELETE,為了簡(jiǎn)單起見(jiàn),我使用了 Dictionary 來(lái)存取數(shù)據(jù),你可以用 瀏覽器 或者 Postman 或者 Fiddler 進(jìn)行測(cè)試,請(qǐng)注意,這里為了方便,我在 Post 方法中使用了硬編碼,實(shí)際場(chǎng)景中你可以用自己的方式生成唯一ID。

          接下來(lái)的章節(jié)我們將會(huì)學(xué)習(xí)如何使用 RestSharp 去調(diào)用剛才構(gòu)建的 API 接口。

          安裝 RestSharp

          要想使用 RestSharp,你可以使用 Visual Studio 2019 中的 NuGet package manager 可視化界面進(jìn)行安裝,或者通過(guò) NuGet package manager console 命令行輸入如下命令:


          Install-Package?RestSharp

          使用 RestSharp 調(diào)用 ASP.NET Core API

          一旦 RestSharp 成功引用到項(xiàng)目之后,就可以使用它了,首先, 你需要?jiǎng)?chuàng)建 RestClient 實(shí)例,下面的代碼展示了如何對(duì) RestClient 進(jìn)行實(shí)例化和初始化操作,要注意的是構(gòu)造函數(shù)中的 url 配置的是 基址,言外之意這不是完整的url。


          RestClient?client?=?new?RestClient("http://localhost:58179/api/");

          接下來(lái),你可以傳遞 資源名請(qǐng)求方式 兩個(gè)參數(shù)來(lái)實(shí)例化 RestRequest 對(duì)象,下面的代碼展示了如何實(shí)現(xiàn)。


          RestRequest?request?=?new?RestRequest("Default",?Method.GET);

          最后,你可以執(zhí)行 request 請(qǐng)求,再將返回的結(jié)果序列化, 最后用一個(gè)合適的對(duì)象接收,就像下面代碼一樣。


          IRestResponsestring>>?response?=?client.Executestring>>(request);

          下面是完整的可供參考的代碼清單。


          using?RestSharp;
          using?System;
          using?System.Collections.Generic;
          namespace?RESTSharpClientDemo
          {
          ????class?Program
          ????{
          ????????private?static?RestClient?client?=?new?RestClient("http://localhost:58179/api/");
          ????????
          ????????static?void?Main(string[]?args)
          ????????{
          ????????????RestRequest?request?=?new?RestRequest("Default",Method.GET);
          ????????????IRestResponsestring>>?response?=?client.Executestring>>(request);
          ????????????Console.ReadKey();
          ????????}
          ????}
          }

          如果想使用 RestSharp 發(fā)送 POST 請(qǐng)求,可以使用如下代碼。


          RestRequest?request?=?new?RestRequest("Default",?Method.POST);
          request.AddJsonBody("Robert?Michael");
          var?response?=?client.Execute(request);

          RestSharp 可以跨多個(gè) .NET 平臺(tái)使用,比如說(shuō):Momo,Xarmain,Blazer 等等,這也是它為什么非常流行的原因,而且 RestSharp 支持通過(guò)泛型方式獲取結(jié)果,這個(gè)特性特別 ????,想了解更多 RestSharp 知識(shí),可參考 Github:https://github.com/restsharp/RestSharp

          譯文鏈接:https://www.infoworld.com/article/3489481/how-to-consume-an-aspnet-core-web-api-using-restsharp.html

          瀏覽 29
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91POR中文字幕 | 人人看人人爱视频 | 美女裸体无遮挡天天摸天天做 | 俺也去俺也去俺也去俺也去 | 青青操免费 |