Problem
The following WebAPI method should be implemented:
/api/books?author=XXX&title=XXX&isbn=XXX&somethingelse=XXX&date=XXX
All query string parameters are allowed to be null. That is, the caller can specify any number of parameters from 0 to all five.
I used to do the following in the MVC4 beta:
public class BooksController : ApiController
{
// GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
public string GetFindBooks(string author, string title, string isbn, string somethingelse, DateTime? date)
{
// ...
}
}
This is no longer the case in MVC4 RC. If I specify fewer than 5 parameters, I get a 404 error message that says:
What is the correct method signature to have it behave as it did before, without needing to utilize the optional argument in the URL routing?
Asked by frapontillo
Solution #1
This problem has been resolved in MVC4’s standard release. You can now accomplish the following:
public string GetFindBooks(string author="", string title="", string isbn="", string somethingelse="", DateTime? date= null)
{
// ...
}
Everything will function right away.
Answered by frapontillo
Solution #2
As vijay noted, many parameters can be supplied as a single model. When using the FromUri argument attribute, this works for GET. This instructs WebAPI to populate the model with query parameters.
With only one parameter, the outcome is a clearer controller operation. See http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api for further details.
public class BooksController : ApiController
{
// GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
public string GetFindBooks([FromUri]BookQuery query)
{
// ...
}
}
public class BookQuery
{
public string Author { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public string SomethingElse { get; set; }
public DateTime? Date { get; set; }
}
It even accepts numerous parameters as long as the attributes are not incompatible.
// GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
public string GetFindBooks([FromUri]BookQuery query, [FromUri]Paging paging)
{
// ...
}
public class Paging
{
public string Sort { get; set; }
public int Skip { get; set; }
public int Take { get; set; }
}
Update: For the model properties, utilize reference types or nullables (ex. int?) to ensure that the values are optional.
Answered by Andrew C
Solution #3
For all parameters, use the default values as shown below.
public string GetFindBooks(string author="", string title="", string isbn="", string somethingelse="", DateTime? date= null)
{
// ...
}
Answered by Muhammad Amin
Solution #4
You can develop a model instead of passing several parameters if you want to pass many parameters.
If you don’t wish to supply any parameters, you can skip them as well, and your code will be cleaner.
Answered by vijay
Solution #5
For options that aren’t marked as ‘optional,’ default values can’t be provided.
Function GetFindBooks(id As Integer, ByVal pid As Integer, Optional sort As String = "DESC", Optional limit As Integer = 99)
In your WebApiConfig
config.Routes.MapHttpRoute( _
name:="books", _
routeTemplate:="api/{controller}/{action}/{id}/{pid}/{sort}/{limit}", _
defaults:=New With {.id = RouteParameter.Optional, .pid = RouteParameter.Optional, .sort = UrlParameter.Optional, .limit = UrlParameter.Optional} _
)
Answered by Rizwan Mumtaz
Post is based on https://stackoverflow.com/questions/11862069/optional-query-string-parameters-in-asp-net-web-api