Reputation: 121
I have this function in jQuery
var uri = "api/queries";
function test(){
var params = {
origin: $('#depair').val(),
destination: $('#destair').val(),
departure_date: $('#depdate').val(),
currency: $('#currency').val(),
}
$.getJSON(uri, params)
.done(function (data) {
console.log(data);
});
}
Which sends the request to this Controller
:
public class QueriesController : ApiController
{
[HttpGet]
public string GetInfo()
{
return "blah";
}
}
So, the request looks like this
http://localhost:55934/api/queries?origin=&destination=&departure_date=¤cy=
How do I access the parameters of the request from inside the controller GetInfo
method?
Upvotes: 1
Views: 14393
Reputation: 39966
You can use Model Binding. First create a class
(ViewModel
) like this:
public class Querie
{
public string Origin { get; set; }
public string Destination { get; set; }
public string Departure_date { get; set; }
public string Currency { get; set; }
}
Then include this class as parameter to your method like this:
public class QueriesController : ApiController
{
[HttpGet]
public string GetInfo(Querie querie)
{
//querie.Origin
return "blah";
}
}
Model binding maps data from HTTP requests to action method parameters. The parameters may be simple types such as strings, integers, or floats, or they may be complex types. This is a great feature of MVC because mapping incoming data to a counterpart is an often repeated scenario, regardless of size or complexity of the data.
Upvotes: 3
Reputation: 189
var origin = Request.QueryString["origin"];
Replacing "origin" with your parameter.
Upvotes: 3
Reputation: 2100
You can include them as parameters to your function.
[HttpGet]
public string GetInfo(string origin, string destination, string departure_date, string currency)
{
return "blah";
}
Upvotes: 5