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

Andrus

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
    }
}
Badri

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Front end ASP.NET MVC4 as one project, and an ASP.NET Web API as another project in same solution - how to call WebAPI from front end?

From Dev

How to create custom web.config in ASP.Net MVC4?

From Dev

How to use ASP.Net MVC4 Web API with AngularJS partial views (aka ng-include)

From Dev

How to catch undefined api method calls in ASP.NET MVC4 Web API

From Dev

How to create customers api using ASP.NET MVC4 and Web API for predefined json result

From Dev

Multiple remote validation attribute in ASP.NET MVC4

From Dev

Custom Headers and Signalr: Asp.Net MVC4 Web Api

From Dev

how to create manifest if bundling and minification is used in ASP.NET MVC4

From Dev

How to create roles and add users to roles in ASP.NET MVC Web API

From Dev

How to render view to string in ASP.NET MVC4 Api controller

From Dev

Stuck with the Google API YouTube Search example in ASP.NET MVC4?

From Dev

How to create a dymanic function in ASP.NET MVC with Web API and EF

From Dev

How to Send Image as Base64String using Alamofire POST request and how to handle the request in Asp.net MVC4 web Api Controller?

From Dev

How to create common method for web api Url in asp.net MVC4

From Dev

Having multiple get-methods with multiple query string parameters in ASP.NET Core Web Api

From Dev

Asp.net MVC4 Parallel Programming for multiple API calls

From Dev

Update my ASP.NET MVC4 project templates?

From Dev

Front end ASP.NET MVC4 as one project, and an ASP.NET Web API as another project in same solution - how to call WebAPI from front end?

From Dev

Asp.Net MVC4 Web API - Do we need OData for building a fast query service

From Dev

How to run a asp.net MVC4 web application in RedHat OS

From Dev

How to catch undefined api method calls in ASP.NET MVC4 Web API

From Dev

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

From Dev

How to grab GET parameters in ASP.NET web API?

From Dev

How to create a dymanic function in ASP.NET MVC with Web API and EF

From Dev

How to make a generic base controller with generic parameters in asp.net mvc4

From Dev

How to create common method for web api Url in asp.net MVC4

From Dev

How to do Attribute Routing in ASP.Net MVC4 and Web API 1 Project?

From Dev

Asp.net MVC4 Parallel Programming for multiple API calls

From Dev

How to integrate Bing Cognitive Web Search API in website of asp.net MVC

Related Related

  1. 1

    Front end ASP.NET MVC4 as one project, and an ASP.NET Web API as another project in same solution - how to call WebAPI from front end?

  2. 2

    How to create custom web.config in ASP.Net MVC4?

  3. 3

    How to use ASP.Net MVC4 Web API with AngularJS partial views (aka ng-include)

  4. 4

    How to catch undefined api method calls in ASP.NET MVC4 Web API

  5. 5

    How to create customers api using ASP.NET MVC4 and Web API for predefined json result

  6. 6

    Multiple remote validation attribute in ASP.NET MVC4

  7. 7

    Custom Headers and Signalr: Asp.Net MVC4 Web Api

  8. 8

    how to create manifest if bundling and minification is used in ASP.NET MVC4

  9. 9

    How to create roles and add users to roles in ASP.NET MVC Web API

  10. 10

    How to render view to string in ASP.NET MVC4 Api controller

  11. 11

    Stuck with the Google API YouTube Search example in ASP.NET MVC4?

  12. 12

    How to create a dymanic function in ASP.NET MVC with Web API and EF

  13. 13

    How to Send Image as Base64String using Alamofire POST request and how to handle the request in Asp.net MVC4 web Api Controller?

  14. 14

    How to create common method for web api Url in asp.net MVC4

  15. 15

    Having multiple get-methods with multiple query string parameters in ASP.NET Core Web Api

  16. 16

    Asp.net MVC4 Parallel Programming for multiple API calls

  17. 17

    Update my ASP.NET MVC4 project templates?

  18. 18

    Front end ASP.NET MVC4 as one project, and an ASP.NET Web API as another project in same solution - how to call WebAPI from front end?

  19. 19

    Asp.Net MVC4 Web API - Do we need OData for building a fast query service

  20. 20

    How to run a asp.net MVC4 web application in RedHat OS

  21. 21

    How to catch undefined api method calls in ASP.NET MVC4 Web API

  22. 22

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

  23. 23

    How to grab GET parameters in ASP.NET web API?

  24. 24

    How to create a dymanic function in ASP.NET MVC with Web API and EF

  25. 25

    How to make a generic base controller with generic parameters in asp.net mvc4

  26. 26

    How to create common method for web api Url in asp.net MVC4

  27. 27

    How to do Attribute Routing in ASP.Net MVC4 and Web API 1 Project?

  28. 28

    Asp.net MVC4 Parallel Programming for multiple API calls

  29. 29

    How to integrate Bing Cognitive Web Search API in website of asp.net MVC

HotTag

Archive