Possible Duplicate:
Can I have an optional parameter for an ASP.NET SOAP web service
I have tried looking around for this but I really cannot find the right solution for what I want to do. Sorry for the repetition since I know this has been asked before. I have a Web Service Method in which I want to have an optional parameter such as:
public void MyMethod(int a, int b, int c = -3)
I already know I cannot use nullable parameters, so I am just trying to use a default value. Problem is when I call this method without supplying argument c is still get an exception. Is there somewhere I have to specify it is in fact optional?
Thanksю
Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.
By using Method Overloading: You can implement optional parameters concept by using method overloading. In method overloading, we create methods with the same name but with the different parameter list.
Flexible Methods With Optional Arguments You may want to make some of your arguments optional by providing a default value. Now you can call write with 2 arguments, in which case mode will equal the default value ( "w" ), or you can pass in 3 arguments to override the default value & get different results.
What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.
You can achieve optional parameters by overloads and using the MessageName property.
[WebMethod(MessageName = "MyMethodDefault")] public void MyMethod(int a, int b) { MyMethod( a, b, -3); } [WebMethod(MessageName = "MyMethod")] public void MyMethod(int a, int b, int c)
For this to work, you may also need to change
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
to
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
if you haven't done so already, as WS-I Basic Profile v1.1
does not allow overloads
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With