18

I just started using WCF with REST and UriTemplates. Is it now possible to use optional parameters?

If not, what would you guys recommend I do for a system that has three parameters that are always used in the url, and others that are optional (varying amount)?

Example:

https://example.com/?id=ID&type=GameID&language=LanguageCode&mode=free 
  • id, type, language are always present
  • mode is optional
abatishchev
  • 98,240
  • 88
  • 296
  • 433
chobo
  • 31,561
  • 38
  • 123
  • 191
  • There is nothing special to be done. Just check for string.IsNullOrEmpty for the mode parameter and if it is null or empty, assign a default value. – amit_g Apr 25 '11 at 18:18
  • I've the same problem, in .NET 4... There must be something to set somewhere... ANy hint? – Rabskatran Nov 23 '12 at 11:13

3 Answers3

31

I just tested it with WCF 4 and it worked without any problems. If I don't use mode in query string I will get null as parameter's value:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = "GetData?data={value}&mode={mode}")]
    string GetData(string value, string mode);
}

Method implementation:

public class Service : IService
{
    public string GetData(string value, string mode)
    {
        return "Hello World " + value + " " + mode ?? "";
    }
}

For me it looks like all query string parameters are optional. If a parameter is not present in query string it will have default value for its type => null for string, 0 for int, etc. MS also states that this should be implemented.

Anyway you can always define UriTemplate with id, type and language and access optional parameters inside method by WebOperationContext:

var mode = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["mode"];
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    @John: Good question. I tried the `int` and edited my answer. – Ladislav Mrnka Apr 25 '11 at 18:50
  • Thanks. Was getting confused cause I read about a lot of conflicting information regarding this. – chobo Apr 25 '11 at 19:06
  • I tried it earlier and it dint work, and now it works. Look at the question below http://stackoverflow.com/questions/2969238/optional-query-string-parameters-in-uritemplate-in-wcf – genericuser Jun 01 '11 at 15:48
2

I have tried with optional parameters in restful web service, If we do not pass anything in parameter value it remains null. After that we can check for the null or empty in function. If it is null then don't use it, else you can use it. Let say I have below code

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate = "GetSample?part1={part1}&part2={part2}")]
    string GetSample(string part1, string part2);
}

Here part1 is compulsory and part2 is optional. Now the function would look like

public class Service : IService
{
    public string GetSample(string part1, string part2)
    {
        if (!string.IsNullOrEmpty(part2))
        {
            return "Hello friends..." + part1 + "-" + part2;
        }
        return "Hello friends..." + part1;
    }
}

You can also make the conversion based on your requirements.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
Satyen Pandya
  • 43
  • 1
  • 8
-1

You have to use "?" followed by "/" in your Url.

example:

[WebGet(UriTemplate = "GetSample/?OptionalParamter={value}")]
    string GetSample(string part1);
Shakeer Hussain
  • 2,230
  • 7
  • 29
  • 52