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.

7
  • $http.get can take only query parameters, are you meaning to do this - this.API_URL_BASE + '/api/getItems?dateRange=' + dateRange ? Commented Jun 12, 2017 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? Commented Jun 12, 2017 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. Commented Jun 12, 2017 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? Commented Jun 12, 2017 at 11:01
  • 1
    have you tried this: stackoverflow.com/questions/42929376/… Commented Jun 12, 2017 at 11:15

2 Answers 2

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){
}
Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

Hi Zoop, I am using angular 2 (version 4). Maybe someone else will find it useful though. Thanks anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.