REST with null types?

I hit a brick wall. My REST implementation is not 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); 

Where is my original SOAP implementation. So is there a way around this? Or do I need to rewrite my code?

I still don't understand why datetime must be NULL in order to be set to null.

+8
c # soap rest nullable
source share
2 answers

Variables for UriTemplate query values ​​must have types that can be converted by QueryStringConverter. There are no null types.

You can wrap the parameters and pass them 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); 

Again, you can pass the date as a string instead of a DateTime, and then use DateTime.Parse () in the string on the receiving side.

+5
source share

The problem is that you are trying to convert the value of the query string to NULL, since in true SOAP your request will be XML that supports nullables.

If you firmly maintain the structure of your method, and CheckedDate is indeed optional, you should change it as an optional parameter.

GetMethod(...., DateTime CheckStartDate = default(DateTime), DateTime CheckEndDate = default(DateTime))

and then in your method check CheckedDate > DateTime.MinValue

0
source share

All Articles