I'm building a web API using web service.
Users may use it like this:
http://www.example.com/example.asmx/hello?param1=str¶m2=str
or:
http://www.example.com/example.asmx/hello?param1=str
.
I want to make param1 required while param2 optional.But my code below always throws an exception that says missing values for parameters when I try to call http://www.example.com/example.asmx/hello?param1=str
. It works fine with http://www.example.com/example.asmx/hello?param1=str¶m2=str
.
[WebMethod]
public string hello(int param1, int param2 = 0)
{
return "hello!";
}
Is there any way to fix it? If not, what techniques can I use to build a web API that accept optional parameters which is very common in public APIs. I'm a newbie so I don't know if web service is a good choice for building web APIs. Any help is appreciated.