2023年3月27日 星期一

[研究][ASP.NET]用 RestSharp v107 做 RESTful API 呼叫(二)模組化

[研究][ASP.NET]用 RestSharp v107 做 RESTful API 呼叫(二)模組化

2023-03-26

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

RestSharp v107開始語法大改,和 106.x差異很大,108.x 、109.x繼續延續 107.x。

注意 RestSharp 107 和 106 無法共存,一旦升級上去,RestSharp 106 的語法就攤了,必須立刻改寫。

********************************************************************************

Web.Config 設定一些變數值

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ProductRestfulURL" value="https://www.ncert.nat.gov.tw/" />
    <add key="Account" value="帳號" />
    <add key="Pass" value="密碼" />
  </appSettings>
</configuration>

********************************************************************************

建立 Models 目錄,加入/新增項目,選程式碼,選類別,把 Class1.cs 改名 MyMoldel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1.Models
{
    public class MyModel
    {
    }
	public class ProductListResult
    {
        public string message { get; set; }
        public string describe { get; set; }
        public List<ProductListData> data { get; set; }
    }

    public class ProductListData
    {
	public string product_id { get; set; }
        public string product_name { get; set; }
        public string product_level { get; set; }
        public string product_official_type { get; set; }
        public string product_type { get; set; }
        public string product_sub_type { get; set; }
        public string product_adjust_note { get; set; }
    }
}


********************************************************************************

建立 Logic 目錄,加入/新增項目,選程式碼,選類別,把 Class1.cs 改名 ProductRestSharp107Client.cs

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using WebApplication1.Models;

namespace WebApplication1.Logic
{
    public class ProductRestSharp107Client
    {
        static readonly string productRestfulURL = ConfigurationManager.AppSettings["ProductRestfulURL"];
static readonly string productRestfulAccount = ConfigurationManager.AppSettings["Account"]; static readonly string productRestfulPass = ConfigurationManager.AppSettings["Pass"];
        public static async Task<GetUnitListResult> GetUnitListResult()
        {

            var data = new Dictionary<string, string>
                {
                    { "code", productRestfulAccount },
{ "pass", productRestfulPass },
}; HttpClient client = System.Net.Http.HttpClientFactory.Create(); string url = "https://www.xxx.idv.tw/rest/getProductList"; var content = new FormUrlEncodedContent(data); var response = await client.PostAsync(url, content); if (response.IsSuccessStatusCode) { var responseVerifyResultString = await response.Content.ReadAsStringAsync(); GetUnitListResult responseUnitListResultJson = JsonConvert.DeserializeObject<GetUnitListResult>(responseVerifyResultString); return responseUnitListResultJson; } else { return null; } } } }

********************************************************************************

Default2.aspx

<%@ Page Language="C#" Async="true" AutoEventWireup="true" 
    CodeBehind="Default2.aspx.cs" Inherits="WebApplication1.Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

Default2.aspx.cs

using System;
using WebApplication1.Logic;

namespace WebApplication1
{
    public partial class Default2 : System.Web.UI.Page
    {
        // 注意,這裡要 async
        protected async void Page_Load(object sender, EventArgs e)
        {
            //ProductRestSharp107Client client = new ProductRestSharp107Client();
var result = await ProductRestSharp107Client.GetUnitListResult();
Label1.Text = result.message; foreach (var item in result.data) { Response.Write( item.unit_name + "<br />"); } } } }

(完)

相關

[研究][ASP.NET]用 RestSharp v107 做 RESTful API 呼叫(二)模組化
https://shaurong.blogspot.com/2023/03/aspnet-restsharp-v107-restful-api_27.html

[研究][ASP.NET]用 HttpClientFactory 做 RESTful API 呼叫
https://shaurong.blogspot.com/2023/03/aspnet-httpclientfactory-restful-api.html

[研究]ASP.NET RESTful API: 比較 HTTPClient , RestSharp , WebClient, HttpClientFactory, Flurl, Refit, RestEase
https://shaurong.blogspot.com/2023/03/aspnet-restful-api-httpclient-restsharp.html

[研究][ASP.NET]RestSharp 106 升級 v107疑難排解:未包含 HasValue 的定義
https://shaurong.blogspot.com/2023/03/aspnetrestsharp-106-v107-hasvalue.html

[研究][ASP.NET]用 RestSharp v107 做 RESTful API 呼叫
https://shaurong.blogspot.com/2023/03/aspnet-restsharp-v107-restful-api.html

[研究][ASP.NET]用 HttpClient 做 RESTful API 呼叫
https://shaurong.blogspot.com/2023/03/aspnet-httpclient-restful-api.html

[研究][ASP.NET]ChatGPT,請提供完整 RestSharp v107 範例程式https://shaurong.blogspot.com/2023/03/aspnetchatgpt-restsharp-v107.html


沒有留言:

張貼留言