我尝试使用 AngularJS 和使用 Web Api 的 C# Controller 制作联系页面。
上周我一直在研究它,但没有找到适合我的正确答案。
我也尝试通过 AJAX、JQUERY 和 Angular 发帖。
Controller 代码:
[HttpPost, AllowAnonymous]
[Route("ContactUs")]
public void Post(ContactUsProperties EmailDetails)
{
//Code for sending email to the website owner...
}
Angular JS 代码:
app.controller("ContactController", function ($scope, $http) {
$scope.EmailDetails = {
Phone: null,
Email: null,
Name: null,
Subject: null,
Body: null
};
$scope.EmailDetailsArr = [
$scope.Phone,
$scope.Email,
$scope.Name,
$scope.Subject,
$scope.Body
];
$scope.EmailDetails = {
Phone: $scope.Phone,
Email: $scope.Email,
Name: $scope.Name,
Subject: $scope.Subject,
Body: $scope.Body
};
$scope.CreateEmail = function () {
console.log($scope.EmailDetails);
console.log(JSON.stringify($scope.EmailDetails));
$http({
method: 'POST',
url: '/ContactUs',
data: JSON.stringify($scope.EmailDetail)
});
}
路由正常,我知道这是因为每次我单击“发送”按钮时调试器都会阻止我。我尝试将其作为数组、JSON、XML 和 Everything,对象仍然为空!我尝试使用 [FROMBODY] 仍然没有。
老实说,如果 POST 需要更改 Web Api 配置,所以我没有更改..
请大家帮我解决这个问题,说明会愉快地接受。 谢谢:)
最佳答案
您应该使用[FromBody]
以获得Parameter Binding .
When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter.
在这个例子中,内容类型是application/json
。
public void Post([FromBody] ContactUsProperties EmailDetails)
关于javascript - Angular post 和 web api C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46791770/