2016-12-13 27 views
0

我有一個名爲Service1.cs包含C#文件中:如何調用一個簡單的Web服務

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

什麼網址必須去去訪問我的testMethod()返回的字符串?

我試過http://localhost:8000/helloworld/testmethod,http://localhost:8000/helloworld/Service1/testmethod, 但我無法弄清楚如何到達從Web服務返回的字符串值。

在此先感謝。

+0

其中是您的項目/應用程序中的代碼,您正在使用webservice的含義,意思是「Service1」,您需要創建一個Web服務的實例,重新使用以調用webservice'testMethod()'在C#WebService上做一個簡單的谷歌搜索教程很多很好的例子都在線以及 – MethodMan

回答

2

您可以用鼠標右鍵單擊該項目 - >引用並單擊添加服務引用並粘貼URL,

enter image description here

創建一個客戶喜歡這一點,並調用方法:

myService.Service1Client client = new myService.Service1Client(); 
var result = client.testMethod(); 
+0

在哪個文件中我寫客戶端代碼?只需訪問url就可以訪問字符串結果嗎? –

+0

你可以寫在任何應用程序,winform的.cs文件或任何asp.netnet文件 – Sajeetharan

+0

是否有可能像這樣對服務的AJAX調用? $ http({method:'GET',url:'/ testMethod' })。success(deferred.resolve).error(deferred.reject); –

相關問題