1

I have a webmethod like this:

[WebMethod(EnableSession = true)]
public string testMethod(string param1, string param2="default value")
{
    .......
}

I am trying to callthis method using jquery omitting the optional parameter, and the webmethod doesn't get hit.

$.ajax({
    type: "POST",
    url: url,
    data: { "param1": "value1" }
}).complete(function (data) {....});  

I works only when I pass the second parameter also. Is there any way to solve this ?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Sharun
  • 3,022
  • 6
  • 30
  • 59
  • possible duplicate of [Can I have an optional parameter for an ASP.NET SOAP web service](http://stackoverflow.com/questions/1723052/can-i-have-an-optional-parameter-for-an-asp-net-soap-web-service) – Raidri May 27 '14 at 10:38

1 Answers1

4

I don't think you can use Optional Params with webservice methods, you'll probably need to Overload the method...

[WebMethod(EnableSession = true, MessageName = "testMethod"))]
public string testMethod(string param1, string param2)
{
      .......
}

[WebMethod(EnableSession = true, MessageName = "testMethod")]
public string testMethod(string param1)
{
      testMethod(param1, "default value");
}

EDIT: Article Link.

There's also an article here worth reading for additional approaches, and although it concludes not to use Overloading, I guess it's your decision.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82