By Trylling


2014-02-28 08:57:55 8 Comments

I'm trying to get filtered data from server using a filtering object I pass to the server side. I have managed to get this working with a post:

angular:

var filter: { includeDeleted: true, foo: bar };
$http({ method: 'post', url: 'api/stuff', data: filter });

web api:

public IEnumerable<StuffResponse> Post([FromBody]Filter filter)
{
    return GetData(filter);
}

But i don't want to use a post for this, I want to use a get. But this does not work:

angular

$http({ method: 'get', url: 'api/stuff', params: filter });

web api

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

Also tried specifying params: { filter: filter }. If i try [FromBody] or nothing, filter is null. With the FromUri i get an object at least - but with no data. Any ideas how to solve this, without creating input parameters for all filter properties?

4 comments

@sanjeewa 2017-09-19 16:46:13

you can send object with Get or Post method .

Script

//1.
var order = {
     CustomerName: 'MS' };
//2.
var itemDetails = [
     { ItemName: 'Desktop', Quantity: 10, UnitPrice: 45000 },
     { ItemName: 'Laptop', Quantity: 30, UnitPrice: 80000 },
     { ItemName: 'Router', Quantity: 50, UnitPrice: 5000 }
];
//3.
$.ajax({
     url: 'http://localhost:32261/api/HotelBooking/List',
     type: 'POST',
     data: {order: order,itemDetails: itemDetails},
     ContentType: 'application/json;utf-8',
     datatype: 'json'
    }).done(function (resp) {
        alert("Successful " + resp);
    }).error(function (err) {
        alert("Error " + err.status);});

API Code

[Route("api/HotelBooking/List")]
[HttpPost]
public IHttpActionResult PostList(JObject objData)
{  List<ItemDetails > lstItemDetails = new List<ItemDetails >();
   dynamic jsonData = objData;
   JObject orderJson = jsonData.itemDetails;
   return Ok();}

@Norbert Norbertson 2019-05-23 13:21:40

that's a POST. Does it work with a get? Plus it's not angular JS.

@Ibrahim Kais Ibrahim 2015-02-04 12:09:12

Yes you can send data with Get method by sending them with params option

var data ={
  property1:value1,
  property2:value2,
  property3:value3
};

$http({ method: 'GET', url: 'api/controller/method', params: data });

and you will receive this by using [FromUri] in your api controller method

public IEnumerable<StuffResponse> Get([FromUri]Filter filter)
{
    return GetData(filter);
}

and the request url will be like this

http://localhost/api/controller/method?property1=value1&property2=value2&property3=value3

@Vil 2015-11-12 05:19:28

Solved it this way:

Angular:

$http({
       url: '/myApiUrl',
       method: 'GET',
       params: { param1: angular.toJson(myComplexObject, false) }
      })

C#:

[HttpGet]
public string Get(string param1)
{
     Type1 obj = new JavaScriptSerializer().Deserialize<Type1>(param1);
     ...
}

@Ramesh Rajendran 2014-02-28 09:22:04

A HTTP GET request can't contain data to be posted to the server. What you want is to a a query string to the request. Fortunately angular.http provides an option for it params.

See : http://docs.angularjs.org/api/ng/service/$http#get

@Trylling 2014-02-28 10:20:39

As you see in the example with 'get', I am using 'params'. And I can see the filter object is translated to URL parameters in the request - but the server can't deserialize it to the Filter object

@Ramesh Rajendran 2014-02-28 10:23:48

Filter is a class name, So you can't do this, you can't pass class in a query string, so you need to change the class to string,int,float...

@testpattern 2015-12-09 09:19:58

Vil's answer provides a cleaner solution imo

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] ASP.NET Web Site or ASP.NET Web Application?

58 Answered Questions

[SOLVED] How to get the value from the GET parameters?

18 Answered Questions

[SOLVED] Get the size of the screen, current web page and browser window

11 Answered Questions

9 Answered Questions

29 Answered Questions

[SOLVED] How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

3 Answered Questions

[SOLVED] Web API Get Method with Complex Object as Parameter

3 Answered Questions

Passing and retrieving complex objects from Angularjs to Web Api

Sponsored Content