Search code examples
c#web-serviceswcfuritemplatewebget

WCF: Multiple optional parameters in UriTemplate


The UriTemplate of my Service looks like this:

[WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/email/{email}")]

The function looks like this:

public User LoginUser(string authorizationCode, string userId, string intend, string email)

My usual call looks like this:

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy/email/abc@web.de

My next task is to make the intend and email parameter optional.

So my call has to work in all of these variants:

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy/email/abc@web.de

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/intend/yyy

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519/email/abc@web.de

http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519

How do I edit my template and function to make it work?


Solution

  • You can set the default values of intend and email to null, as shown below:

            [OperationContract]
            [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend=null}/{email=null}")]
             string LoginUser(string authorizationCode, string userId, string intend, string email);
    

    We can access this service via the URI below:

    http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/

    http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/int/

    http://localhost:8000/LoginUser/authorizationCode/Test/userId/Test/intend/int/ema/

    In this scenario, if you want to set email, you must set the intend.

    So I think the best way is to write an interface for them separately like below:

            [OperationContract]
            [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/email/{email}")]
             string LoginUser1(string authorizationCode, string userId, string intend, string email);
            [OperationContract]
            [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/intend/{intend}/")]
            string LoginUser2(string authorizationCode, string userId, string intend);
            [OperationContract]
            [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/email/{email}")]
            string LoginUser3(string authorizationCode, string userId, string email);
            [OperationContract]
            [WebGet(UriTemplate = "LoginUser/authorizationCode/{authorizationCode}/userId/{userId}/")]
            string LoginUser4(string authorizationCode, string userId);
    

    Different interfaces will be requested for different URIS;