Web API 2: how to search

In ASP Web API 2, I would like to implement a search function in my REST URI.

For example, if I have a Clients resource

/base_url/customers
/base_url/customers/1
....

I would like to implement, for example:

/base_url/customers?active=true

How can I implement a search in the Web API 2 controller? (I don’t want to use the OData protocol because I have a DTO object: my controller should interact with the DTO object, and not directly with the model objects).

+4
source share
3 answers
  • Define a class of search parameters with all the properties you want your client to search for. Lets call it CustomerSearchOptions for now:

    public class CustomerSearchOptions
    {
        public bool IsActive { get; set; }
    
        public string AnotherProperty {get; set;}
    }
    
  • Get api, CustomerSearchOptions, , [FromUri].

  • get (MyCustomerDto):

        [HttpGet]
        [ResponseType(typeof(List<MyCustomerDto>))]
        public async Task<IHttpActionResult> SearchAsync([FromUri] CustomerSearchOptions searchOptions)
        {
            if (searchOptions == null)
            {
                return BadRequest("Invalid search options");
            }
    
            var searchResult = await myRepo.SearchAsync(searchOptions);
    
            return Ok(searchResult);
        }
    
    1. - api api, NOT .

/base_url/customers? isActive = true & anotherProperty = somevalue

.

, .

+6

URL- , , , api . , .

, , , . URL-, ! , POST ... /api/entity/query .

POST , .

+2

All Articles