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.
-
stackoverflow.com/questions/15852859/…I4V– I4V2013年05月27日 14:58:02 +00:00Commented May 27, 2013 at 14:58
-
@l4V Thanks! Seems I need to learn something about WCF. I'll give it a try and see if it works.ChandlerQ– ChandlerQ2013年05月27日 15:08:33 +00:00Commented May 27, 2013 at 15:08
-
You may want to think about using ASP.NET Web API. I'm pretty sure it handles that case correctly and it's designed specifically for building Web APIs.Youssef Moussaoui– Youssef Moussaoui2013年05月27日 15:08:52 +00:00Commented May 27, 2013 at 15:08
-
@YoussefMoussaoui I'll google something about ASP.NET Web API, thanks for your help.ChandlerQ– ChandlerQ2013年05月27日 15:12:11 +00:00Commented May 27, 2013 at 15:12
-
@YoussefMoussaoui Thanks for your help! I've tried Web API and it works like a charm! That's exactly what I'm looking for.ChandlerQ– ChandlerQ2013年05月30日 03:48:38 +00:00Commented May 30, 2013 at 3:48
3 Answers 3
basically you cant do that.
i suggest you to read this article first: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
it explains pretty well how parameters are bound.
2 Comments
have you considered to use params which works within applications (wpf / winforms).
// not tested
[WebMethod]
public string hello(params int[] list)
{
string s = "Hello\n";
// do some stuff with your ints
for ( int i = 0 ; i < list.Length ; i++ )
s += list[i] + "\n" ;
return s;
}
1 Comment
1http://www.example.com/example.asmx/hello?userid=a1b2&count=5
. Both the paremeter name and parameter value are needed. I suppose your method doesn't support name/value type parameters.The problem probably is that for web method this parameter is required. Maybe this will help.
In accordance with MinOccurs Attribute Binding Support and Default Attribute Binding Support:
Value type accompanied by a public bool field that uses the Specified naming convention described previously under Translating XSD to source - minOccurs value of output
<element>
element 0.
result:[WebMethod] public SomeResult SomeMethod(bool optionalParam, [XmlIgnore] bool optionalParamSpecified)
<s:element minOccurs="0" maxOccurs="1" name="optionalParam" type="s:boolean" />
Value type with a default value specified via a System.Component.DefaultValueAttribute - minOccurs value of output
<element>
element 0. In the<element>
element, the default value is also specified via the default XML attribute.
result:[WebMethod] public SomeResult SomeMethod([DefaultValue(true)] bool optionalParam)
<s:element minOccurs="0" maxOccurs="1" default="true" name="optionalParam" type="s:boolean" />