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