我正在嘗試編寫一個C#寧靜的Web服務,它在帳戶編號作爲參數傳遞時返回客戶名稱。將字符串返回到自定義對象類型
我有一個客戶類:
public class Customer_T
{
public string CustName { get; set; }
}
接口類:
[ServiceContract(Namespace = "", Name = "CustomerInfoService")]
public interface CustomerInfo_I
{
[OperationContract]
Customer_T CustName(string accountno);
}
*另一個叫CustomerInfo類,它實現CustomerInfo_I接口:*
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class CustomerInfo : CustomerInfo_I
{
[WebInvoke(Method = "GET", UriTemplate = "customer/{accountno}", ResponseFormat = WebMessageFormat.Json)]
public Customer_T CustName(string accountno)
{
string custName = "";
CustNameByAccountNo custdb = new CustNameByAccountNo();
custName = custdb.getCustName(accountno).ToString();
if (custName.Equals("") == false)
{
return new Customer_T { CustName = custName };
}
else
{
return null; //This is where I want to change
}
}
}
而不是返回null,我想返回一個字符串「InvalidAccNo」。
我嘗試過,但它給了我一個錯誤。
Cannot implicitly convert type 'string' to 'CustomerInfo.Service.Customer_T'
你爲什麼要返回Customer_T是你想返回一個字符串? – SimpleVar 2012-04-23 07:32:37
Customer_T具有在該類中定義的CustName屬性。如果沒有客戶名稱被返回,我想顯示字符串「InvalidCustAcc」。我不希望將字符串分配給CustName屬性。 – cookiemonsta 2012-04-23 09:35:45