I'm really struggling with this one. I need a generic list parameter for my Get method, but it needs to be optional. I just did this:
public dynamic Get(List <long> ManufacturerIDs = null)
Unfortunately on runtime i get the error:
Optional parameter 'ManufacturerIDs' is not supported by 'FormatterParameterBinding'.
How to get a generic list as an optional parameter here?
JohnnyHK
313k70 gold badges631 silver badges477 bronze badges
asked Mar 26, 2013 at 15:12
1 Answer 1
What's the point of using an optional parameter? List<T>
is a reference type and if the client doesn't supply a value it will simply be null:
public HttpResponseMessage Get(List<long> manufacturerIDs)
{
...
}
answered Mar 26, 2013 at 15:30
3 Comments
Augusto Barreto
In my experience, there are many cases where you have to explicitly set optional parameters to null (stackoverflow.com/a/22397723/1454888). This is counter-intuitive for me, but it works. Thanks.
Nebula
I am using swashbuckle for swagger documentation generation. In that context it does matter how the optional parameters are defined, observe:
[FromUri] object[] foo = null
--> "Provide multiple values in new lines." [FromUri] object[] foo
--> "Provide multiple values in new lines (at least one required)." object[] foo
--> "(required)" So in that context it really depends on what you expect from your end user.pawellipowczan
I had to insert [FromUri] attribute before the List<string> optional parameter. There is a difference when you use = null as @Nebula noticed. When you don't add = null then you have to specify at least one element.
default