3

I'm building a web API using web service. Users may use it like this: http://www.example.com/example.asmx/hello?param1=str&param2=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&param2=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.

Darin Dimitrov
1.0m275 gold badges3.3k silver badges2.9k bronze badges
asked May 27, 2013 at 14:54
5
  • stackoverflow.com/questions/15852859/… Commented 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. Commented 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. Commented May 27, 2013 at 15:08
  • @YoussefMoussaoui I'll google something about ASP.NET Web API, thanks for your help. Commented 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. Commented May 30, 2013 at 3:48

3 Answers 3

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.

answered May 27, 2013 at 15:08
Sign up to request clarification or add additional context in comments.

2 Comments

I don't think he's using Web API at all.
regarding to sample uri's, then yes, i agree.
0

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;
 }
answered May 27, 2013 at 16:06

1 Comment

well,parameters here are name/value pair. Say, 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.
0

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:

  1. 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.

    [WebMethod]
    public SomeResult SomeMethod(bool optionalParam, [XmlIgnore] bool optionalParamSpecified)
    result:
    <s:element minOccurs="0" maxOccurs="1" name="optionalParam" type="s:boolean" />

  2. 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.

    [WebMethod]
    public SomeResult SomeMethod([DefaultValue(true)] bool optionalParam)
    result:
    <s:element minOccurs="0" maxOccurs="1" default="true" name="optionalParam" type="s:boolean" />

answered Dec 13, 2017 at 1:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.