I am authoring a RESTful WCF service using .Net 4.0. I want the following two URLS:
/root/document/{ids}?fields={fields}
/root/externaldocument/{ids}?fields={fields}
to map to the same interface member:
Documents GetDocuments(string ids, string fields)
I have tried putting a wildcard into a literal URL segment:
[OperationContract]
[WebGet(UriTemplate = "/root/*document/{ids}?fields={fields}")]
Documents GetDocuments(string ids, string fields)
However, this is not valid and I get the following exception:
The UriTemplate '/root/*document/{ids}?fields={fields}' is not valid; the
wildcard ('*') cannot appear in a variable name or literal... Note that a
wildcard segment, either a literal or a variable, is valid only as the last
path segment in the template
If I wrap the wildcard segment in a template brace:
[OperationContract]
[WebGet(UriTemplate = "/root/{*document}/{ids}?fields={fields}")]
Documents GetDocuments(string ids, string fields)
Then I get an exception because there is no such input parameter in the method arguments:
Operation 'GetDocuments' in contract 'IAPIv2' has a UriTemplate that expects a
parameter named 'DOCUMENTS', but there is no input parameter with that name
on the operation.
My workaround is simply to have two entries, pointing to different methods, and then have the methods call a common implementation:
[OperationContract]
[WebGet(UriTemplate = "/root/document/{ids}?fields={fields}")]
Documents GetDocuments(string ids, string fields)
[OperationContract]
[WebGet(UriTemplate = "/root/externaldocument/{ids}?fields={fields}")]
Documents GetExternalDocuments(string ids, string fields)
But this seems kind of ugly.
I have read the documentation and cannot find this point specifically address. Is there any way I can have a wildcard literal segment in WCF? Or is this not possible in WCF?