-1

In my Web API application, I want to pass multiple condition to filter out data in database. Those condition will be pass from UI.

string dateRangeType = "XYZ", string startDate = "", string endDate = ""

So how can I combine those 3 parameters into single object and used in Web API GET method in C#

DevPerson
  • 349
  • 1
  • 4
  • 17
  • How?? It all depends on how the web API is accepting the parameters. You need to show us code of the web API and also the code of how you are calling the web API. – Chetan Ranpariya Oct 09 '17 at 04:56

3 Answers3

2

You can create a model class and use it as the parameter of the web api controller. For example:

public class MyDateDTO
{
    public String dateRangeType { get; set; }
    public String startDate { get; set; }
    public String endDate { get; set; }
}

Next in your web api controller

    [HttpGet]
    public String MyDateAction([FromUri]MyDateDTO dto)//must put FromUri or else 
        //the web api action will try to read the reference type parameter from
        //body
    {
        //your code
    }

Also note that you have to put FromUri in order to be able to read the reference type object from the query parameter as by default the action will try to read it from the body. More details here.

Tayyab
  • 1,174
  • 8
  • 26
0

You can add all these 3 properties in a class and then pass that object as parameter and then post data using body will do the job

public class MyContract {
    public string dateRangeType;
    public string startDate;
    public string  endDate;
}

Change the signature of your action to pass MyContract object as a parameter with [FromBody] attribute, and then pass data as JSON in your request body like this -

{
   dateRangeType: "abc",
   startDate: "2017-09-10",
   endDate: "2017-09-11"
}
Ipsit Gaur
  • 2,614
  • 1
  • 19
  • 34
-2

In Get you can not pass single object, for this you need to convert method into POST method. WebApi Accepts data via FormFactory and via JSON Factory. These two convert request data into object for post only not for GET. For GET we need only parameters as single as method input parameters. You can check also Model Binder which follows the same approach.

Vijay Raheja
  • 262
  • 2
  • 10