如何调用简单的Web服务

时间:2016-12-13 16:43:10

标签: 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";
    }
}

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

<WebService Language="c#" Codebehind="Service1.cs"
    Class="Service1.Service1">

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

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

我试过http://localhost:8000/helloworld/testmethodhttp://localhost:8000/helloworld/Service1/testmethod, 但我无法弄清楚如何获取从Web服务返回的字符串值。

提前致谢。

1 个答案:

答案 0 :(得分:4)

您可以右键单击项目 - &gt;参考并点击添加服务参考并粘贴网址

enter image description here

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

myService.Service1Client client = new myService.Service1Client();
var result = client.testMethod();
相关问题