2

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
    }
}
1
  • Your immediate issue could be solved by adding a route the WebApiConfig for "api/{controller}/{action}/" but that does not improve the code. Commented Mar 17, 2014 at 3:35

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.