I have a Web API and a very quick page I threw together to test it (the actual page that will call the API is/will be written by an outside vendor).
My Search action takes several parameters, all of which are optional and should default to empty strings.
[ActionName("Search")]
public IEnumerable<Foo> Get(string param1 = "", string param2 = "", string param3 = "")
And the View that calls the API:
@using (Ajax.BeginForm(new AjaxOptions {
Url = ViewBag.APIUrl + "api/controller/Search",
HttpMethod = "GET",
OnSuccess = "successful" }))
{
@Html.TextBox("param1")
@Html.TextBox("param2")
@Html.TextBox("param3")
}
The GET request looks like this
[...]/Search?param1=¶m2=¶m3=
Every value that is empty on the form is being passed to the API Controller as null, and thus the default values of empty strings are not being used. (It does pass actual values as expected.) As it is I'm null-coalescing these into empty strings after the fact, but the old version of this API did not have to do that.
When I add
int skip = 0, int take = 0
as parameters, I get the
The parameters dictionary contains a null entry
exception. I know this can be fixed by making those params nullable, then null-coalescing them like the strings. But purpose-wise, they should not be nullable, they should be optional with a default value.
How do I make it use the default values of the parameters in the action? Am I doing something wrong in the form, or in the API Controller?