Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
    }
}
share|improve this question
    
Your immediate issue could be solved by adding a route the WebApiConfig for "api/{controller}/{action}/" but that does not improve the code. –  Andrew McLachlan Mar 17 '14 at 3:35

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.