Reputation: 344
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/[email protected]
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/[email protected]
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/[email protected]
http://localhost:60522/MyService.svc/json/LoginUser/authorizationCode/111222333/userId/0006435cec5aa5640639b5d08314bb989a43519
How do I edit my template and function to make it work?
Upvotes: 0
Views: 389
Reputation: 3954
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;
Upvotes: 2