如何 post 一个对象到 WebAPI
How to post an object to WebAPI
我正在尝试弄清楚如何 post 一个对象从我的表单到 Web api 服务。在我的控制器中,我定义了一个我想添加输入值的模型。
$scope.Label;
在我的输入字段中,我使用 ng-model
绑定它们,例如:
<input type="checkbox" ng-model="label.isPublic" />
<input type="text" ng-model="label.labelName" required focus-me />
在提交表单时,这两个字段被传递到我的服务并提交到我的 WebApi
我尝试过两种方式提交:
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', { params: label }, {
}).then(function (response) {
return response.data;
});
};
也可以如下不声明参数
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', label , {
}).then(function (response) {
return response.data;
});
};
在网络上API 我有一个 post
的方法设置
[Route ("reportLibrary/createlabel/")]
[HttpPost]
public DTOs.ReportLabel CreateLabel(DTOs.ReportLabel json)
{
DTOs.ReportLabel result = new DTOs.ReportLabel();
//.... do stuff
return result;
}
ReportLabel (dto) 定义如下:
public class ReportLabel
{
public Int64 LabelId { get; set; }
public string LabelName { get; set; }
public bool IsPublic { get; set; }
public IEnumerable<Report> Reports { get; set; }//placeholder?
}
我遇到的问题是,当我 post 来自 angular 服务的一个对象时,它在 API 中显示为 null。如果我将方法中的类型更改为 JToken 或 JObject 之类的值,则会出现这些值。
任何人都可以帮助我理解为什么当我定义它没有从 angular 传递过来的类型时?
谢谢
您似乎多了一步。您不需要在 json 中编码然后在 json
中传入
return $http.post('reportLibrary/createlabel/', { LabelId: 101, LabelName: 'myname' }, {
然后
public DTOs.ReportLabel CreateLabel([FromBody]ReportLabel reportLabel)
查看经过的网络值,您应该在调试工具或 fiddler 中看到实际发布的值(表单值)。
我正在尝试弄清楚如何 post 一个对象从我的表单到 Web api 服务。在我的控制器中,我定义了一个我想添加输入值的模型。
$scope.Label;
在我的输入字段中,我使用 ng-model
绑定它们,例如:
<input type="checkbox" ng-model="label.isPublic" />
<input type="text" ng-model="label.labelName" required focus-me />
在提交表单时,这两个字段被传递到我的服务并提交到我的 WebApi
我尝试过两种方式提交:
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', { params: label }, {
}).then(function (response) {
return response.data;
});
};
也可以如下不声明参数
function addLabel(label) {
var mylabel = encodeURIComponent(angular.toJson(label));
return $http.post('reportLibrary/createlabel/', label , {
}).then(function (response) {
return response.data;
});
};
在网络上API 我有一个 post
的方法设置 [Route ("reportLibrary/createlabel/")]
[HttpPost]
public DTOs.ReportLabel CreateLabel(DTOs.ReportLabel json)
{
DTOs.ReportLabel result = new DTOs.ReportLabel();
//.... do stuff
return result;
}
ReportLabel (dto) 定义如下:
public class ReportLabel
{
public Int64 LabelId { get; set; }
public string LabelName { get; set; }
public bool IsPublic { get; set; }
public IEnumerable<Report> Reports { get; set; }//placeholder?
}
我遇到的问题是,当我 post 来自 angular 服务的一个对象时,它在 API 中显示为 null。如果我将方法中的类型更改为 JToken 或 JObject 之类的值,则会出现这些值。
任何人都可以帮助我理解为什么当我定义它没有从 angular 传递过来的类型时?
谢谢
您似乎多了一步。您不需要在 json 中编码然后在 json
中传入return $http.post('reportLibrary/createlabel/', { LabelId: 101, LabelName: 'myname' }, {
然后
public DTOs.ReportLabel CreateLabel([FromBody]ReportLabel reportLabel)
查看经过的网络值,您应该在调试工具或 fiddler 中看到实际发布的值(表单值)。