博客
关于我
.Net中WebService的Demo示例
阅读量:427 次
发布时间:2019-03-06

本文共 2017 字,大约阅读时间需要 6 分钟。

创建并调用Web服务

一、创建一个Web服务

  • 创建一个新的项目

    首先,在Visual Studio中创建一个新的项目,选择“Web项目”模板,项目名称可以命名为WebServerDemo

  • 在项目中添加新的项

    接着,在项目的文件资源管理器中,右键点击项目,选择“新建”,然后添加一个“Web服务”项。这样,Visual Studio会自动为你创建一个基本的Web服务文件,通常命名为TestServer.asmx

  • 编辑TestServer.asmx文件

    打开TestServer.asmx文件,查看其默认代码。默认代码如下:

    using System;
    using System.Web.Services;
    using System.ComponentModel;
    using System.Web;
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class TestServer : System.Web.Services.WebService
    {
    [WebMethod]
    public string HelloWorld()
    {
    return "Hello World";
    }
    }

    这段代码定义了一个基本的Web服务类TestServer,其中包含一个方法HelloWorld(),返回“Hello World”的字符串。

  • 添加新的方法

    为了使Web服务功能更强大,可以添加一个新的方法。右键点击项目,选择“新建”,然后选择“函数”(Method),选择“Web方法”(Web Method),然后选择一个合适的方法名,比如GetAge。这样,Visual Studio会自动生成一个新的方法,代码如下:

    [WebMethod]
    public string GetAge(string id)
    {
    return $"ID为:{id}的年龄为:{new Random().Next(10, 41)}";
    }
  • 运行TestServer.asmx页面

    在Visual Studio中,右键点击项目,选择“运行”,然后选择URL选项,输入http://localhost:port/TestServer.asmx(其中port是Visual Studio指定的端口号)。这样,你就可以在浏览器中看到并测试你的Web服务是否正常运行了。


  • 二、使用.NET调用Web服务

  • 创建一个新页面

    在项目中添加一个新的页面,选择“Web页面”模板,命名为WebServerData.aspx。右键点击该文件,选择“查看代码”,然后在Page_Load事件中添加以下代码:

    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);
    }

    这段代码创建了一个TestServerSoapClient对象,调用了HelloWorldGetAge方法,并将结果输出到页面上。

  • 测试是否调用成功

    保存修改后,运行WebServerData.aspx页面,检查浏览器中是否显示了预期的输出。如果输出为空,可能需要检查Web服务的配置或客户端引用是否正确。


  • 三、使用前端JS调用Web服务

  • 创建一个新的HTML页面

    在项目中添加一个新的HTML文件,命名为WebServerData.html。在<head>标签中添加必要的元标签和脚本引用,然后在<body>中添加一个点击按钮和一个显示结果的区域。

    调用Web服务
    获取Web服务数据

    这段代码使用了JQuery库通过AJAX技术调用GetAge方法,并将结果显示在页面上。

  • 测试前端调用是否成功

    保存修改后,打开WebServerData.html页面,点击按钮,检查是否能够显示出预期的年龄数据。如果没有显示结果,可能需要检查网络连接或者脚本引用是否正确。


  • 通过以上步骤,你已经成功创建并调用的一个简单的Web服务,并且学习了如何在不同技术栈中使用它。

    转载地址:http://cwmkz.baihongyu.com/

    你可能感兴趣的文章
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>