为什么我将 post JSON 转换为 web API 方法时得到 null?
Why I get null when I make post JSON to web API method?
我创建并从 cilent 发送 JSON 到 web api method.But 我在端点函数中得到 NULL。
我有这个功能:
function genarateDirectives(workPlanServise) {
var dataObj = {
name: 'name',
employees: 'employee',
headoffice: 'master'
};
return workPlanServise.generateFilter(dataObj)
.then(function (result) {
return result.data;
});
}
这是我使用的服务:
(function () {
"use strict";
angular.module("workPlan").factory("workPlanServise", ["$http", "config", workPlanServise]);
function workPlanServise($http, config) {
var serviceUrl = config.baseUrl + "api/workPlan/";
var service = {
getAll: getAll,
getSubGridContent: getSubGridContent,
generateFilter:generateFilter
};
return service;
function getAll() {
return $http.get(serviceUrl);
}
function getSubGridContent(clientId) {
return $http.get(serviceUrl + '?clientId=' + clientId);
}
function generateFilter(objData) {
return $http.post(serviceUrl, objData );
}
}
})();
这里端点 web api 函数:
[HttpPost]
public async Task<IHttpActionResult> Post([FromBody]string objData)
{
try
{
return null;
}
catch (Exception)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
}
知道为什么 objData 总是 null 吗?
创建一个模型class,其中的字段与您网站中 objData 中的字段相匹配api。
Web api 模型活页夹将为您填充。不要忘记检查请求是否在 header 中包含 contentType: "application/json"。 (一个标准的 $http 调用会有那个)
例如:
public class SomeModel
{
public string Name { get; set; }
public int Number { get; set; }
public string Description { get; set; }
}
然后 post 它到:
[HttpPost]
public async Task<IHttpActionResult> Post(SomeModel objData)
{ ....
或
如果您确实需要 post 一个字符串到 Web api,您需要在请求的 header 中传递 text/plain 而不是 [=29] =] 并向您的网站 api 添加一个额外的 text/plain 格式化程序。 See here for more info
因为您将 JSON 对象绑定到无效的字符串。创建模型
public class MyModel
{
public string Name { get; set; }
public string Employees { get; set; }
public string HeadOffice { get; set; }
}
并在没有 [FromBody] 属性的操作中使用它,因为默认情况下所有引用类型都从正文绑定。
public async Task<IHttpActionResult> Post(MyModel objData)
{
// work with objData here
}
我创建并从 cilent 发送 JSON 到 web api method.But 我在端点函数中得到 NULL。
我有这个功能:
function genarateDirectives(workPlanServise) {
var dataObj = {
name: 'name',
employees: 'employee',
headoffice: 'master'
};
return workPlanServise.generateFilter(dataObj)
.then(function (result) {
return result.data;
});
}
这是我使用的服务:
(function () {
"use strict";
angular.module("workPlan").factory("workPlanServise", ["$http", "config", workPlanServise]);
function workPlanServise($http, config) {
var serviceUrl = config.baseUrl + "api/workPlan/";
var service = {
getAll: getAll,
getSubGridContent: getSubGridContent,
generateFilter:generateFilter
};
return service;
function getAll() {
return $http.get(serviceUrl);
}
function getSubGridContent(clientId) {
return $http.get(serviceUrl + '?clientId=' + clientId);
}
function generateFilter(objData) {
return $http.post(serviceUrl, objData );
}
}
})();
这里端点 web api 函数:
[HttpPost]
public async Task<IHttpActionResult> Post([FromBody]string objData)
{
try
{
return null;
}
catch (Exception)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
}
知道为什么 objData 总是 null 吗?
创建一个模型class,其中的字段与您网站中 objData 中的字段相匹配api。
Web api 模型活页夹将为您填充。不要忘记检查请求是否在 header 中包含 contentType: "application/json"。 (一个标准的 $http 调用会有那个)
例如:
public class SomeModel
{
public string Name { get; set; }
public int Number { get; set; }
public string Description { get; set; }
}
然后 post 它到:
[HttpPost]
public async Task<IHttpActionResult> Post(SomeModel objData)
{ ....
或
如果您确实需要 post 一个字符串到 Web api,您需要在请求的 header 中传递 text/plain 而不是 [=29] =] 并向您的网站 api 添加一个额外的 text/plain 格式化程序。 See here for more info
因为您将 JSON 对象绑定到无效的字符串。创建模型
public class MyModel
{
public string Name { get; set; }
public string Employees { get; set; }
public string HeadOffice { get; set; }
}
并在没有 [FromBody] 属性的操作中使用它,因为默认情况下所有引用类型都从正文绑定。
public async Task<IHttpActionResult> Post(MyModel objData)
{
// work with objData here
}