Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass optional parameters for web method?

Tags:

c#

asp.net

wcf

I have a web method with multiple parameters. The web method is only dependent on 2 fields, the rest are all optional.

   [OperationContract]
    public string WarehouseContactInformation(int WAID (Required), string CN (Required), string CT (Optional), string CC (Optional), string CFN (Optional), string CD (Optional), string CE (Optional),string CW (Optional))

How to I declare these parameters as optional so that when I call the Web Method I only have to pass through the fields that i have values for, example:

WarehouseContactInformation(1,'Bill','00012311')
WarehouseContactInformation(1,'Bill','00012311','12415415','123525')
like image 866
Jacques Bronkhorst Avatar asked Apr 03 '13 10:04

Jacques Bronkhorst


People also ask

How do you pass optional parameters to a method?

By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method.

Can we make Web API method parameters as optional?

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.

How do I add an optional parameter to a URL?

Adding Optional Parameters to your Order Page URLs To add a third parameter, add another ampersand & , then the name of the parameter, and then the value for the parameter.

How do I set optional parameters?

Using Optional Attribute Here for the [Optional] attribute is used to specify the optional parameter. Also, it should be noted that optional parameters should always be specified at the end of the parameters. For ex − OptionalMethodWithDefaultValue(int value1 = 5, int value2) will throw exception.


2 Answers

You can't. Web methods doesn't support optional parameters. When you generate proxy for web method, you make get the specific signature, according to which you client and server would exchange the messages. But it can't pass the optional parameters. You can use default parameters on the server side, but no optional.

like image 143
Alex Avatar answered Oct 10 '22 21:10

Alex


What i did is: send the parameter binded with XML, and don't bind the values of optional parameters leave that blank.

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(
            "<registration>" +
            "<field1>" + value + "</field1>" +
            "<field2>" + value(or leave blank) + "</field2>" +
            "<field3>" + value + "</field3>" +
            "<field4>" + value + "</field4>" +
            "</registration>");

        int status = objectOfService.methodName(xmlDoc);

and in web service you can do like

    public int UpdateUser(XmlNode node)
    {
       String filed1Value=node["field1"].InnerText;
    }

Hope it helps.

like image 45
Mogli Avatar answered Oct 10 '22 21:10

Mogli