Reputation: 4896
When calling a REST service with jquery, I can pass a POJO object to $.get(...) call, and jquery will serialize the object's properties to GET QueryString.
My REST service is developed using WebAPI, and I'm wondering if I can make it automatically deserialize querystring parameters to an object.
More precisely:
If I have a POST method, I can deserialize the json from message body as below
I have the following jQuery code
var dataObj // my POJO
j$.post('/api/myserv', dataObj, function (data, status, jqXHR) {
// ....
And the WebApi endpoint
' MyEntity class has same structure as POJO object
Public Function PostValue(<FromBody()> ByVal entity As MyEntity) As HttpResponseMessage
' here I have entity instantiated and setup
When using jquery get, and passing POJO object:
var dataObj // my POJO
j$.get('/api/myserv', dataObj, function (data, status, jqXHR) {
// ....
This translates to
GET /api/myserv?prop1=val1&prop2=val2&...
In WebApi I would like to instantiate an object as with POST
' MyEntity class hadata from uri, s same structure as POJO object
Public Function GetValue(<FromURI()> ByVal entity As MyEntity) As HttpResponseMessage
' but here the entity is null
To be able to read I have to have a param for every prop in POJO object, like
' MyEntity class hadata from uri, s same structure as POJO object
Public Function GetValue(prop1 As .., prop2 as ..., ) As HttpResponseMessage
Is there any way to be able to read all params from get in a single object, as it'spossib;e with POST from body?
Thank you
Upvotes: 0
Views: 1263
Reputation: 18759
You can use [FromUri]
before the object in the WebAPI
method parameter. Take a look at Parameter Binding
here.
Set your jquery call up like below:
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
data: { prop1: 1, prop2: "two" },
success: function (result) {
//handle
}
});
Upvotes: 3