问题描述
伊夫打了砖墙。我的REST实现不会接受空的值。
Ive hit a brickwall. My REST implementation wont accept Nullable values.
[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实现呢。那么,有没有办法解决这?还是必须重新写我的代码?
Where as my origional SOAP implementation does. So is there a way round this? Or do I have to re-write my code?
我还是不很明白为什么一个日期必须是可空反正被设置为null。
I still dont quite get why a datetime must be nullable anyway to be set to null.
推荐答案
有关UriTemplate查询的值的变量必须具有可以通过QueryStringConverter转换类型。 。可空类型是不是
Variables for UriTemplate query values must have types that can be converted by QueryStringConverter. Nullable types is not.
您可以换的参数,并通过它通过POST这样;
You could wrap the parameters and pass it through POST as such;
[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);
Opionally,您可以通过日期作为字符串而非日期时间,然后使用DateTime.Parse()上在接收端的串
Opionally, you could pass the date as strings instead of DateTime, and then use DateTime.Parse() on the string on the receiving end.
这篇关于REST可空类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!