首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

使用 C# 建立 Web Service(二):美元兌人民幣匯率轉換服務完整程式碼實作

     編輯:關於C語言

Service1.asmx 完整程式碼


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    public class Service1 : System.Web.Services.WebService
    {
        public Service1() { InitializeComponent(); }

        #region Component Designer generated code
        private IContainer components = null;
        private void InitializeComponent() { }
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null) components.Dispose();
            base.Dispose(disposing);
        }
        #endregion

        // Web Service:美元轉人民幣
        [WebMethod]
        public double DollarConvertToRMB(double Dollar)
        {
            return Dollar * 8.15; // 假設固定匯率
        }
    }
}

關鍵說明

  • [WebMethod] 屬性標記公開方法
  • 回傳 double,參數 double Dollar
  • 匯率 8.15 為示範固定值,實務應連接資料庫取得即時匯率
  • 必要命名空間:System.Web、System.Web.Services

部署步驟

  1. 儲存為 Service1.asmx(.asmx 為 .NET Web Service 標準副檔名)
  2. 放置於虛擬目錄(如 C:\Inetpub\wwwroot\WebService1)
  3. 瀏覽器測試:https://localhost/WebService1/Service1.asmx
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved