随笔 - 81  文章 - 0  评论 - 514  阅读 - 41万

.net中WebService的使用实例

一、创建一个Webwebservice

      1.新建一个项目WebserverDemo

      2.在项目处添加新建项,添加一个web服务

   

  3.编辑TestServer.asmx文件

    3.1 TestServer.asmx默认的代码是这样

复制代码
/// <summary>
    /// TestServer 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class TestServer : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        
    }
复制代码

3.2 现在加多一个方法  

       [WebMethod]
        public string GetAge(string id)
        {
            return "ID为:" + id + "的年龄为:"+new Random().Next(10,41);
        }

4.运行TestServer.asmx页面,看到下图这样一个Webserver就创建成功了

二、.net调用Webwebservice

   通常是把WebServer发布到iis,然后在另一个程序中调(这里为了方便直接在本程序中调用演示)

  1.项目中的引用选择添加服务引用,地址输入刚才那个页面的地址。

 

 然后看项目Service References文件夹

2.新建一个WebServerData.aspx页面,在.cs中写

 protected void Page_Load(object sender, EventArgs e)
        {
            ServiceReference1.TestServerSoapClient testServer = new ServiceReference1.TestServerSoapClient();
            string str1= testServer.HelloWorld();
            string str2 = testServer.GetAge("b101");
            Response.Write(str1 + "," + str2);
        }

 有结果输出刚调用成功了。

三、前端JS调用Webwebservice

 1.把TestServer.asmx 文件的允许ajax调用web服务下面一行代码取消注释

 2.添加一个WebServerData.html页面

  

复制代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src=" http://libs.baidu.com/jquery/1.11.1/jquery.min.js "></script>
        <script type="text/javascript">
            $(function () {
                $("#getdata").click(function () {
                    $.ajax({
                        type: 'POST',
                        url: 'TestServer.asmx/GetAge',
                        data: '{ id:"bb101"}',
                        dataType: 'json',
                        contentType: "application/json",
                        success: function (data) {
                            $("#data").append(data.d);
                        }
                    });
                });
            });
        </script>
</head>
<body>
    <a id="getdata" href="javascript:void(0);">获取webservice数据</a>
    <div id="data"></div>
</body>
</html>
复制代码

 点击a显示下图则成功。

posted on   包子wxl  阅读(12683)  评论(3编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏