溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

.net實(shí)現(xiàn)webservice的實(shí)例介紹

發(fā)布時(shí)間:2021-08-24 17:43:36 來源:億速云 閱讀:119 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“.net實(shí)現(xiàn)webservice的實(shí)例介紹”,在日常操作中,相信很多人在.net實(shí)現(xiàn)webservice的實(shí)例介紹問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”.net實(shí)現(xiàn)webservice的實(shí)例介紹”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

原理:WebService是一個(gè)SOA(面向服務(wù)的編程)的架構(gòu),它是不依賴于語言,不依賴于平臺(tái),可以實(shí)現(xiàn)不同的語言間的相互調(diào)用,通過Internet進(jìn)行基于Http協(xié)議的網(wǎng)絡(luò)應(yīng)用間的交互。
作用:主要用于數(shù)據(jù)交換、提供服務(wù)接口
優(yōu)點(diǎn):可跨平臺(tái)、部署簡單調(diào)用方便、無需額外客戶端支持

一、創(chuàng)建一個(gè)WebService服務(wù)
1.創(chuàng)建一個(gè)普通的Asp.Net Web應(yīng)用程序,名稱為WebServiceDemo

2.在剛創(chuàng)建的web程序里添加一個(gè)WebService服務(wù)文件,名稱為TestService.asmx, 步驟看截圖

.net實(shí)現(xiàn)webservice的實(shí)例介紹

3.編寫TestService.asmx服務(wù)代碼,代碼如下

復(fù)制代碼 代碼如下:


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

namespace WebService
{
    /// <summary>
    /// TestService 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://aaa.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
    // [System.Web.Script.Services.ScriptService]
    public class TestService : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int i, int j)
        {
            return i + j;
        }
    }
}

4.進(jìn)行編譯。然后F5運(yùn)行剛創(chuàng)建的項(xiàng)目,我這邊的url是:http://localhost:50639/testservice.asmx , 如果能看到下面截圖就說明創(chuàng)建成功。

.net實(shí)現(xiàn)webservice的實(shí)例介紹

二、后臺(tái)調(diào)用方式
1.添加服務(wù)引用(可以添加一個(gè)新項(xiàng)目或在當(dāng)前WebServiceDemo項(xiàng)目里做引用測(cè)試,這里我直接在WebServiceDemo做引用測(cè)試)

步驟:

a) 右擊WebServiceDemo項(xiàng)目里的引用,然后選擇“添加服務(wù)引用”

b) 彈出框地址填寫: http://localhost:50639/TestService.asmx, (注意:這里要換成你自己剛運(yùn)行的服務(wù)地址)

c) 命名空間改為“TestServiceReference”,然后點(diǎn)擊“往前”按鈕進(jìn)行測(cè)試,如無問題,點(diǎn)擊確定按鈕完成服務(wù)的引用

如下截圖:

.net實(shí)現(xiàn)webservice的實(shí)例介紹

2.在后臺(tái)cs調(diào)用,新建一個(gè)web.aspx頁面,在后臺(tái)鍵入如下代碼進(jìn)行測(cè)試

復(fù)制代碼 代碼如下:


protected void Page_Load(object sender, EventArgs e)
{
    //RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);

    //調(diào)用方法
    TestServiceReference.TestServiceSoapClient testService = new TestServiceReference.TestServiceSoapClient();
    int result = testService.Add(1, 2);
    string hellowWorld = testService.HelloWorld();
    Page.Response.Write("整型結(jié)果:" + result.ToString() + "。字符串結(jié)果:" + hellowWorld);
}

3.如看到結(jié)果,說明WebService的創(chuàng)建和引用成功了

三、前臺(tái)js調(diào)用方式
1.打開TestService.asmx,對(duì)代碼進(jìn)行修改,以適應(yīng)js調(diào)用,代碼如下

復(fù)制代碼 代碼如下:


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

namespace WebService
{
    /// <summary>
    /// TestService 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
    [System.Web.Script.Services.ScriptService]
    public class TestService : System.Web.Services.WebService
    {
        public string GetWeekName(int day)
        {
            string result;
            if (day > 6)
            {
                result = "輸入格式有誤";
            }
            else
            {
                DayOfWeek week = (DayOfWeek)day;
                result = week.ToString();
            }
            return result;
        }
    }
}

2.前端頁面代碼(注意這里需要引入jquery文件)

復(fù)制代碼 代碼如下:


<script type="text/javascript">
    $(function () {
        $.ajax({
            type: 'POST',
            url: 'TestService.asmx/GetWeekName',
            data: '{ day: 4}',
            dataType: 'json',
            contentType: "application/json",
            success: function (data) {
                alert(data.d);
            }
        });
    });
</script>

3.編譯WebService所在項(xiàng)目,然后運(yùn)行我們剛寫好的前端頁面, 結(jié)果如下

.net實(shí)現(xiàn)webservice的實(shí)例介紹

到此,關(guān)于“.net實(shí)現(xiàn)webservice的實(shí)例介紹”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI