- WebGet(UriTemplate = "TestMessage/{p1}?firstname={firstname}&lastname={lastname}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Xml)]
- [OperationContract]
- string TestMessage(string message, string firstname = "", string lastname = "");
Step 2 - Function Definition
- public string TestMessage(string message,string firstname="",string lastname="")
- {
- string responsedata = string.Empty;
- responsedata = message + "" + "FirstName: " + firstname + "" + "LastName: " + lastname;
- return responsedata;
- }
How to call the Service,
Input1
http://localhost:49785/Service.svc/parameter1
Output1 - parameter1 Firstname: LastName
In the above output, only parameter1 will be returned as u didnot specify the other 2 parameters so they will get their value as "Empty"
Input2
http://localhost:49785/Service.svc/parameter1?firstname=kamal&lastname=rawat
Output2 - parameter1 Firstname:kamal LastName:rawat
so u will see in the above output u passed both the parameters so you got all the values.
Input2
http://localhost:49785/Service.svc/parameter1?firstname=kamal
Output2 - parameter1 Firstname:kamal LastName:
In the above output parameter1 is passed i.e firstname=kamal but you wont get lastname parameter, bcoz you didn't pass and its optional if u dont pass the parameter value and will take value as empty.
Thanks for reading..

IndoCRM HRDPosted Jul 28, 2016, 12:06 AM
Same question with Raj Kumah. Let say I allow firstname parameter as optional parameter, while paramater1 and lastname are mandatory. How to get thing done?
Raj KumarPosted Jun 15, 2016, 11:46 AM
Hi Kamal, If i need to pass only the second optional parameter , then how would my url look like ? does it look like this http://localhost:49785/Service.svc/parameter1?lastname=kamal?