【问题标题】:Restful WCF Default valuesRestful WCF 默认值
【发布时间】:2011-07-07 20:41:09
【问题描述】:

ly资源的定义

[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}/{endDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate, string endDate);

如何使最后两个参数可选?即,我希望最后三个调用起作用

"http://domain.com/service.svc/myid/"

"http://domain.com/service.svc/myid/07-07-2011"

"http://domain.com/service.svc/myid/01-01-2011/07-08-2011"

但只有最后一个调用有效,其余的则给出缺少参数的错误。

谢谢 看涨

【问题讨论】:

  • 您的 .NET 框架版本是什么? I tested this beforeUriTemplate 的查询字符串中带有stringint 参数并且它起作用了。

标签: wcf default


【解决方案1】:

我相信您这样做的方式与重载方法调用的方式相同:

[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId);

[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate);

[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}/{endDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate, string endDate);

【讨论】:

  • 我试过了,但它不起作用。我相信它给出了一个错误,即不能有多个与 uripath“getbydaterange”关联的方法。
  • @Bullish:糟糕,更改(或者更确切地说是添加)操作合同的 Name 参数。例如[OperationContract(Name = "GetMyObjectsByInsId")], [OperationContract(Name = "GetMyObjectsByInsIdAndDate")] & [OperationContract(Name = "GetMyObjectsByInsIdAndDateRange")]
  • 是的,这行得通。不是我要找的,但它是最接近的。理想情况下,我希望将一种方法和空值传递给未传入的参数。
  • @Bullish:您仍然可以调用“完整”方法,在您从精简方法中选择的位置传递空参数。即用List&lt;RestfulServiceObj&gt; GetMyObjectsByDateRange(string insId) { return GetMyObjectsByDateRange(insId, default(DateTime), default(DateTime); } 实现第一个方法
猜你喜欢
  • 1970-01-01
  • 2011-01-03
  • 2012-02-05
  • 2012-05-10
  • 2021-03-26
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多