Search code examples
asp.netasp.net-mvcasp.net-mvc-4asp.net-web-apiasp.net-web-api-routing

How to create ASP .NET MVC4 Web API to search my multiple parameters


How to create ASP.NET MVC4 json Web API which allows to search products by id, barcode, search term or retrieve all products since date ?

I tried to use ASP.NET MVC4 controller below.

Calling

http://localhost:52216/admin/api/Products/GetSince?since=2014-03-16%2021:47:29&_=1395007124964

returns error

Multiple actions were found that match the request:

System.Net.Http.HttpResponseMessage GetSince(System.String) on type MyApp.Controllers.ProductsController\r\n


System.Net.Http.HttpResponseMessage GetId(System.String) on type MyApp.Controllers.ProductsController"

How to fix this ? This code looks ugly, it contains number of similar methods. Which is best way to create such API ? How to improve this code ? Http GET method should used but method names and signatures can changed.

ASP.NET/Mono MVC4, jquery, jquery UI are used. Windows 2003 server should also supported, so .NET 4.5 or MVC5 cannot used.

public class ProductsController : ApiController
{
    [HttpGet]
    public HttpResponseMessage GetSince([FromUri]string since))
    {
        var toodelist = GetProducts(since, null, null, null);
        return Request.CreateResponse(HttpStatusCode.OK,
           new { products = toodelist.ToArray() });
    }

    [HttpGet]
    public HttpResponseMessage GetId([FromUri]string id)
    {
        var toodelist = GetProducts(null, null, id, null);
        return Request.CreateResponse(HttpStatusCode.OK,
           new { products = toodelist.ToArray() });
    }


    [HttpGet]
    public HttpResponseMessage GetBarcode([FromUri]string barcode)
    {
        var toodelist = GetProducts(null, barcode, null, null);
        return Request.CreateResponse(HttpStatusCode.OK,
           new { products = toodelist.ToArray() });
    }

    [HttpGet]
    public HttpResponseMessage GetTerm([FromUri]string term)
    {
        var toodelist = GetProducts(null, null, null, term);
        return Request.CreateResponse(HttpStatusCode.OK,
           new { products = toodelist.ToArray() });
    }

    static List<Product> GetProducts(string since, string barcode, string id, string term)
    {
        ... retrieves list of product from database using specified search criteria
        if not null
    }
}

Solution

  • How about using a search criteria DTO like this?

    public class SearchCriteria
    {
        public int? Id { get; set; }
        public DateTime? Since { get; set; }
        // Other properties
    }
    

    Action method will be like this.

    public class ProductsController : ApiController
    {
        public HttpResponseMessage GetProducts([FromUri]SearchCriteria crit))
        {
            // Validate and clean crit object
            var list = GetProducts(crit);
            // return list
        }
    }
    

    GetProducts can return the list of products based on the properties set in SearchCriteria object. If a query string field is not present, corresponding property will be null.