Создайте новый класс ApiController, унаследованный от apiController, добавьте действие JsonContent.
public class ApiCustomController: System.Web.Http.ApiController {
public class JsonContent : ActionMethodSelectorAttribute {
public override bool IsValidForRequest(
ControllerContext controllerContext
, System.Reflection.MethodInfo methodInfo) {
var Request = controllerContext.HttpContext.Request;
string requestedWith = Request.ServerVariables["HTTP_X_REQUESTED_WITH"] ?? string.Empty;
return string.Compare(requestedWith, "XMLHttpRequest", true) == 0
&& Request.ContentType.ToLower().Contains("application/json");
}
}
}
Затем в вашем контроллере наследуйте от пользовательского класса контроллера «ApiCustomController» и используйте атрибут ActionMethodSelectorAttribute «JsonContent».
public class IngredientsController : App_a_matic.Controllers.ApiController {
// GET api/values
[HttpGet]
[JsonContent]
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
}
Это вы отправляете в «contentType: 'application/json'», например:
$(function () {
$.ajax({
url: 'api/Products/Ingredients'
, contentType: 'application/json'
, dataType: 'json'
, type: 'GET'
, success: function (result) {
console.log(result);
}
});
})
В отличие от простого использования getJson.
person
user69841
schedule
27.03.2013