我正在尝试编写一个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