如何调用简单的Web服务

Pay*_*tfi 0 c# asp.net web-services

我有一个名为 Service1.cs 的 C# 文件,其中包含以下内容:

public class Service1 : System.Web.Services.WebService
{ 
    [System.Web.Services.WebMethod]
    public static String testMethod()
    {
       return "Hello";
    }
}
Run Code Online (Sandbox Code Playgroud)

我将这段代码放入我的 helloworld.html 文件中:

<WebService Language="c#" Codebehind="Service1.cs"
    Class="Service1.Service1">
Run Code Online (Sandbox Code Playgroud)

我的目录位于http://localhost:8000/,其中包含 helloworld.html 的链接和下载 Service1.cs 的链接

我必须访问哪个 url 才能访问 testMethod() 返回的字符串?

我尝试过http://localhost:8000/helloworld/testmethodhttp://localhost:8000/helloworld/Service1/testmethod,但我不知道如何获取从 Web 服务返回的字符串值。

提前致谢。

Saj*_*ran 6

您可以右键单击项目 -> 引用 ,然后单击添加服务引用并粘贴 url,

在此输入图像描述

像这样创建一个客户端并调用方法:

myService.Service1Client client = new myService.Service1Client();
var result = client.testMethod();
Run Code Online (Sandbox Code Playgroud)