0

lyDefinition of the resource

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

How can i make the last two parameters optional? i.e, I want the bottom three calls to work

"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"

But only the last call works, the rest give a missing parameter error.

Thanks Bullish

chown
  • 51,908
  • 16
  • 134
  • 170
Bullish
  • 3
  • 2
  • What is your .NET framework version? [I tested this before](http://stackoverflow.com/questions/5781342/wcf-and-optional-parameters) with `string` and `int` parameter in `UriTemplate`'s query string and it worked. – Ladislav Mrnka Jul 08 '11 at 07:40

1 Answers1

0

I believe you do so that same way you overload method calls:

[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);
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • I tried that, but it doesn't work. I believe it gave an error that can not have multiple methods associated with the uripath "getbydaterange". – Bullish Jul 07 '11 at 20:59
  • @Bullish: Oops, change (or rather add) a **Name** parameter to the operation contract. e.g. `[OperationContract(Name = "GetMyObjectsByInsId")]`, `[OperationContract(Name = "GetMyObjectsByInsIdAndDate")]` & `[OperationContract(Name = "GetMyObjectsByInsIdAndDateRange")]` – Brad Christie Jul 08 '11 at 01:59
  • Yup, that worked. Not what I was looking for but it's the closest. Ideally I wanted one method and nulls getting passed to the parameters that were not being passed in. – Bullish Jul 08 '11 at 13:27
  • @Bullish: You can still call the "full" method, passing null parameters in where you so chose from the stripped down method. i.e. Implement the first method with `List GetMyObjectsByDateRange(string insId) { return GetMyObjectsByDateRange(insId, default(DateTime), default(DateTime); }` – Brad Christie Jul 08 '11 at 13:54