WebAPI model binding complex object to [FromUri] sent via query string

I have read a lot of questions about why model binding is not working, but my question is actually the opposite. I'm curious: why does the binding of the model to a complex object happen in the next GET action, even if there is no data in the query string that matches any of the object's properties?

Route configuration:

configuration.Routes.MapHttpRoute(
    "Items- Get By Client ID",
    "clients/{id}/items",
    new
    {
        controller = "Item",
        action = "GetByClientId",
        constraints = new { id = @"\d+" }
    }
);

      

Request parameters class:

public class QueryOptions
{
    public int Skip { get; set; }
    public int Take { get; set; }
}

      

Act:

[HttpGet]
public HttpResponseMessage GetByClientId(int id, [FromUri] QueryOptions queryOptions = null)
{
    // Operations
}

      

example GET

use case:/api/clients/1234/items

This results in an action in which it occurs // Operations

that has a non-null object instance for queryOptions

, with default values ​​for properties. In this case, Skip

and Take

0

.

I would expect that queryOptions

would be null

, unless the property names are not used in the query string, for example: /api/clients/1234/items?skip=0&take=5

. Can anyone explain this behavior?

+3


source to share





All Articles