我碰壁了。我的 REST 实现不接受 Nullable 值。
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Transactions?AccNo={AccNo}&CostCentreNo={CostCentreNo}&TransactionType={TransactionType}&Outstanding={Outstanding}&CheckStartDate={CheckStartDate}&CheckEndDate={CheckEndDate}")]
List<Transactions> GetTransactions(Int32 AccNo, Int32 CostCentreNo, Int32 TransactionType, Boolean Outstanding, DateTime? CheckStartDate, DateTime? CheckEndDate);
而我原来的 SOAP 实现却是这样。那么有办法解决这个问题吗?或者我必须重新编写我的代码?
我仍然不太明白为什么日期时间必须可以为空才能设置为空。
UriTemplate 查询值的变量必须具有可由 QueryStringConverter 转换的类型。可空类型则不然。
您可以包装参数并通过 POST 传递它;
[DataContract(Name = "Details", Namespace = "")]
public class Details
{
[DataMember]
public Int32 AccNo;
[DataMember]
public Int32 CostCentreNo;
[DataMember]
public Int32 TransactionType;
[DataMember]
public Boolean Outstanding;
[DataMember]
public DateTime? CheckStartDate;
[DataMember]
public DateTime? CheckEndDate;
public Details()
{}
}
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Transactions",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<Transactions> GetTransactions(Details details);
或者,您可以将日期作为字符串而不是 DateTime 传递,然后在接收端的字符串上使用 DateTime.Parse() 。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)