REST 具有可空类型?

发布于 2024-12-08 03:50:07 字数 619 浏览 1 评论 0原文

我碰壁了。我的 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 实现却是这样。那么有办法解决这个问题吗?或者我必须重新编写我的代码?

我仍然不太明白为什么日期时间必须可以为空才能设置为空。

I've hit a brickwall. My REST implementation won't 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);

Whereas my original SOAP implementation does. So is there a way around this? Or do I have to re-write my code?

I still don't quite get why a datetime must be nullable anyway to be set to null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2024-12-15 03:50:07

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() 。

Variables for UriTemplate query values must have types that can be converted by QueryStringConverter. Nullable types is not.

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, you could pass the date as strings instead of DateTime, and then use DateTime.Parse() on the string on the receiving end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文