I created a JSON string using jQuery and now I want to send it to a C# web API controller.
Here is an example of the JSON object:
{"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]}
I attempted to send it with a URL structure like this:
API/Recipe/Search?json={"Name":"","Type":"4","Meals":["2","3"],"Excludes":["Beef","Chicken"]}
I tried implementing my Controller in two different ways:
// First approach
public class RecipeController : ApiController {
[HttpGet]
public string Search(searchObject json) {
return "Asdasd";
}
}
// Second approach
public class RecipeController : ApiController {
[HttpGet]
public string Search(string json) {
searchObject search = (searchObject)JsonConvert.DeserializeObject(json);
return "Asdasd";
}
}
However, the controller does not seem to pick up the JSON object in either case. My current framework is MVC4.
Below is the jQuery code snippet that I am using to make the call. The variable `apiLink` represents the link mentioned above.
$.getJSON(apiLink, function (data) {
var items = [];
$.each(data, function (key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
Any suggestions on how I can get the controller to properly receive the JSON object?
Thank you!