我正在嘗試編寫一個將使用(調用)Web服務(Service1.asmx)並顯示結果的應用程序(簡單形式)。現在,Web服務有一個方法。下面是代碼:從ASMX Web服務調用方法-error-
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public Customer getCustomer(String id)
{
Customer customer = new Customer();
customer.CustomerId = id;
customer.CustomerName = "ABC Warehouse";
customer.CustomerAddress = "123 Anywhere";
customer.CustomerCity = "Pittsburgh";
customer.CustomerState = "PA";
customer.CustomerZip = 10379;
customer.CustomerContact = "Dan Smith";
customer.CustomerPhone = "2484567890";
customer.CustomerCredit = "True";
return customer;
}
}
當我運行從原來項目中的Web服務,我能夠在文本框中Example輸入文字,然後點擊調用查看XML結果Example。現在,我在另一個項目中使用的簡單表單有一個文本框(txt1),按鈕(btn1)和標籤(lbl1)。我成功地添加了Web服務以及所有的功能和類傳輸。現在,我想要發生的是當您在文本框中輸入內容時,單擊submit,然後在標籤中查看xml結果,該結果將包含文本框中輸入的文本,就好像我要在其上運行服務一樣擁有。以下是我遇到問題的代碼:
public partial class _Default : System.Web.UI.Page
{
protected void btn1_Click(object sender, EventArgs e)
{
MyService.Service1 service = new MyService.Service1();
string message = service.getCustomer(string id);
ID = txt1.Text;
lbl1.Text = message;
}
}
我在哪裏出錯了?顯然我是初學者,所以所有的幫助將不勝感激。 p.s .:當我添加Web服務時,MyService就是我命名空間的名稱空間
因此,即使如果我自己運行Web服務,它將顯示在xml佈局中,我不打算返回XML佈局? p.s .:我通過添加服務參考 - >高級 - >添加Web參考來使用.asmx服務 – user1830833
什麼是「XML佈局」?我想,如果你真的想要XML,出於某種原因,你可以將MyService.Customer序列化回XML。大多數人很樂意獲得一個類實例,而不是一塊XML。 –