1

I am calling an asp.net mvc web api controller action from an angular 2 application. I can accept objects from the call like this:

    [Route("api/getItems")]
    [HttpPost]
    public ReturnObject GetItems(DateRangeVM dateRange){
    }

However, I do not want to do a post, I want it to be a get, so when I am calling this from angular 2, I want something like this:

return this.http.post(this.API_URL_BASE + '/api/getItems', dateRange, defaultOptions).map((response: Response) => {
      return <any[]>response.json();
    }).catch(this.handleError);
  }

but actually more like this:

return this.http.get(this.API_URL_BASE + '/api/getItems', dateRange, defaultOptions).map((response: Response) => {
      return <any[]>response.json();
    }).catch(this.handleError);
  }

However, this last option does not accept data as the second option. I do not want to pass the data as string arguments because this is too messy when the data becomes more complex.

Vladimir Zdenek
  • 2,270
  • 1
  • 18
  • 23
trees_are_great
  • 3,881
  • 3
  • 31
  • 62
  • `$http.get` can take only query parameters, are you meaning to do this - `this.API_URL_BASE + '/api/getItems?dateRange=' + dateRange` ? – Sajal Jun 12 '17 at 10:58
  • But you shouldn't, sending payload in query can be altered and is not a good practice. Why not stick with `$http.post`? – Sajal Jun 12 '17 at 11:00
  • "I do not want to pass the data as string arguments because this is too messy when the data becomes more complex." The parameter is not string, but a set of objects within another object. – trees_are_great Jun 12 '17 at 11:00
  • @Sajal post i thought should only be done if you are adding or removing data or alike. Seeing as this is a query, it feels wrong to be doing a post. Is it not wrong? – trees_are_great Jun 12 '17 at 11:01
  • Pure REST based approach would be to break the parameters in the object and query them in a string for an `http.get`. There is no harm in using post for all/any operation since its a better protocol for providing better data security. – Sajal Jun 12 '17 at 11:07
  • 1
    have you tried this: https://stackoverflow.com/questions/42929376/bind-query-parameters-to-a-model-in-asp-net-core – Dawood Awan Jun 12 '17 at 11:15
  • Perfect @DawoodAwan that is exactly what I was looking for. thanks – trees_are_great Jun 12 '17 at 12:08

2 Answers2

0

I know this doesn't exactly answer your question, but here is how I approach a similar problem...

Here is a sample Users controller. This method is intended to return all users, or any users that match a specified start date.

public IEnumerable<User> Get(DateTime? startDate = null)
{
    return _userRepo.Get(startDate);
}

In an angular service I defined the following:

function getUsers(startDate) {
    var users = $http({
        method: 'get',
        url: config.remoteServiceName +'users/',
        params: {
            startDate: startDate
        }
    }).then(function (response) {
        var values = [];
        angular.forEach(response.data, function (value) {             
            values.push(User.apiResponseTransformer(value));
        });
        return values;
    }).catch(function (errorResponse) {
        throw errorResponse;
    });

    return users;
}  

Here is a link to another SO question that has been answered which provides some information on why using the request body on a GET is not desirable:

Read content body from a HTTP GET in C# WebAPI

Good luck!

Zoop
  • 872
  • 3
  • 11
  • 24
0

Use FromUri attribute to bind the query string to a complex object. Angular will need to send each property of the class as a query string parameter.

public ReturnObject GetItems([FromUri] DateRangeVM dateRange){
}
Alex Terry
  • 1,992
  • 1
  • 11
  • 17