Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have "overloaded" uritemplates?

        [OperationContract]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Message GetSearchResults(string searchTerm, string searchType);

    [OperationContract]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
    Message GetSearchResults(string searchTerm);

Is this possible - if not, can someone suggest an alternative?

like image 690
Pones Avatar asked Sep 09 '10 11:09

Pones


2 Answers

I've found that this was the best solution for me:

    [OperationContract(Name = "SearchresultsWithSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType=null}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm, string searchType);


    [OperationContract(Name = "SearchresultsWithoutSearchType")]
    [WebGet(UriTemplate = "/searchresults/{searchTerm}", 
    ResponseFormat = WebMessageFormat.Xml)]
    Message GetSearchResults(string searchTerm);

this matches:

"http://myservice/searchresults/mysearchterm"

"http://myservice/searchresults/mysearchterm/"

"http://myservice/searchresults/mysearchterm/mysearchtype"

like image 148
Pones Avatar answered Nov 10 '22 15:11

Pones


No, not really - because the string parameter searchType can be NULL - so you don't really have any way to distinguish the two URL templates. It would be different if you were using a non-nullable type, like an INT or something - then you (and the .NET runtime) could keep the two URL templates apart (based on the fact whether or not the INT is present).

What you need to do is just check whether searchType is empty or NULL in your GetSearchResults method, and act accordingly.

[OperationContract]
[WebGet(UriTemplate = "/searchresults/{searchTerm}/{searchType}", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
Message GetSearchResults(string searchTerm, string searchType);

and in your implementation:

public Message GetSearchResults(string searchTerm, string searchType)
{
   if(!string.IsNullOrEmpty(searchType))
   {
      // search with searchType
   }
   else
   {
      // search without searchType
   }
   ......
}
like image 25
marc_s Avatar answered Nov 10 '22 16:11

marc_s