gpt4 book ai didi

asp.net-mvc - Web api删除方法输入对象参数为空

转载 作者:行者123 更新时间:2023-12-01 19:36:07 25 4
gpt4 key购买 nike

在我的 WebAPI asp.net mvc Controller 删除方法中,传入的名为 contact 的对象为 null。我已经检查了每个地方的代码,但无法找出根本原因。我已经以非常相似的方式成功进行了编辑操作。

那么是什么导致 webapi asp.net 方法中的联系人对象参数作为空值出现?

如图所示,我已检查 Angular Controller 内的联系人对象在传递到 webapi 删除方法之前不为空。

enter image description here

Here is my rest of the code 

<div data-ng-controller="ContactDeleteController">
<form name ="deleteContact" data-ng-submit="saveDeleteContact()">
<div>
<label>First Name: </label>
<input required type="text" placeholder="Enter First Name" data-ng-model="contact.FirstName"/>
</div>
<div>
<label>Last Name: </label>
<input required type="text" placeholder="Enter Last Name" data-ng-model="contact.LastName"/>
</div>
<div>
<label>Email Address: </label>
<input required type="text" placeholder="Enter Email Address" data-ng-model="contact.EmailAddress"/>
</div>
<div>
<label>Cell Phone Number: </label>
<input required type="text" placeholder="Enter Phone Number" data-ng-model="contact.PhoneNumber"/>
</div>
<div></div>
<div>
<button class="btn btn-primary" type="submit">Delete</button>
</div>
</form>
</div>



var ContactDeleteController = function ($scope, $http, $location) {
var contactId = $location.absUrl().match(/\/Delete\/(.*)/)[1];
$http.get("/api/ContactWeb/" + contactId)
.then(function (response) {
$scope.contact = response.data;
});

$scope.saveDeleteContact = function () {
var con = $scope.contact;
$http.delete("/api/ContactWeb", con)
.then(function (response) {
$scope.contact = response.data;
});
window.location = "/Contact/Index";
};
};

enter image description here

最佳答案

HTTP 不允许使用正文进行 DELETE。尝试在 URI(查询字符串等)中发送参数,并在 Web API 中,使用 [FromUri] 将数据绑定(bind)到复杂类型 Contact 中。

关于asp.net-mvc - Web api删除方法输入对象参数为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com