"请求的资源不支持 HTTP 方法 'PUT' ASP.Net Web API
"The requested resource does not support HTTP method 'PUT' ASP.Net Web API
我的 jQuery Put 请求不起作用,当我 运行 我的代码处于调试模式时,put 函数没有被调用,从 FireBug 我可以看到这个错误:
The requested resource does not support HTTP method 'PUT'
这是我的 jQuery 代码和 API 控制器:
<script>
$(document).ready(function() {
$('#success').hide();
function onSelect(e) {
$("#Type").prop("checked", false);
var name = $("#TreeView").data("kendoTreeView").dataItem(e.node).Name;
var accountId =$("#TreeView").data("kendoTreeView").dataItem(e.node).AccountId;
$("#AccountID").val(accountId).prop("disabled",true);
$("#Name").val(name).prop("disabled",true);
var type = $("#TreeView").data("kendoTreeView").dataItem(e.node).Nature;
$("#NatureAccountId").data("kendoDropDownList").value(type);
$("#AccountTypeId").data("kendoDropDownList").value(type);
$("#AccountTypeId").data("kendoDropDownList").enable(false);
$("#BaseCurrencyId").data("kendoDropDownList").value(type);
$("#ContraryToNatureCtrlId").data("kendoDropDownList").value(type);
$("#BudgetCtrlTypeId").data("kendoDropDownList").value(type);
var descrip = $("#TreeView").data("kendoTreeView").dataItem(e.node).Description;
$("#Description").val(descrip);
var idd = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentID;
$("#ParentID").val(idd);
var pName = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentName;
$("#ParentName").val(pName).prop("disabled", true);
var isActive = $("#TreeView").data("kendoTreeView").dataItem(e.node).IsActive;
$("#IsActive").val(isActive);
$("input:radio[name=Type][value='" + type + "']").prop("checked", true);
$("input[name=Type]").prop("disabled", true);
}
var accountchild = {
type: "odata",
transport: {
read: {
url: function(options) {
return kendo.format("/api/Account/GetAccountChilds?ParentID={0}&OrganizationID={1}", options.AccountId, @Session["OrganizationID"].ToString());
}
}
},
schema: {
model: {
id: "AccountId",
hasChildren: "HasChildren",
type: "Type",
name: "Name"
}
}
};
$("#TreeView").kendoTreeView({
dataSource: new kendo.data.HierarchicalDataSource({
type: "odata",
transport: {
read: {
url: "/api/Account/GetAllAccountParrentInfos?OrganizationID=@Session["OrganizationID"].ToString()",
dataType: "json"
}
},
schema: {
data: function(data) {
return data;
},
total: function(data) {
return data['odata.count'];
},
model: {
id: "AccountId",
hasChildren: "HasChildren",
children: accountchild,
type: "Nature",
name: "Name",
Description:"Description",
isActive:"IsActive",
parentName:"ParentName",
ParentID:"ParentID"
}
}
}),
select: onSelect,
dataTextField: ["DisplayName", "DisplayName"],
dataValueField: "AccountId"
});
});
</script>
<script>
$("#AccountTypeId").kendoDropDownList({
dataTextField: "Type",
dataValueField: "Id",
optionLabel: "انتخاب نوع حساب",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetAllAccounttypes"
}
}
}
});
$("#BaseCurrencyId").kendoDropDownList({
dataTextField: "Currency",
dataValueField: "Id",
optionLabel: "انتخاب ارز پایه",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetBaseCurrencies"
}
}
}
});
$("#NatureAccountId").kendoDropDownList({
dataTextField: "Nature",
dataValueField: "Id",
optionLabel: "انتخاب ماهیت حساب",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetNatureAccounts"
}
}
}
});
$("#ContraryToNatureCtrlId").kendoDropDownList({
dataTextField: "Ctrl",
dataValueField: "Id",
optionLabel: "کنترل خلاف ماهیت",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetContraryToNatureCtrls"
}
}
}
});
$("#BudgetCtrlTypeId").kendoDropDownList({
dataTextField: "CtrlType",
dataValueField: "Id",
optionLabel: "نوع کنترل بودجه",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetBudgetCtrlTypes"
}
}
}
});
</script>
<script>
$("#btn_edit").click(function() {
var account = {
"ParentID": $("#ParentID").val(),
"AccountID": $("#AccountID").val(),
"Name": $("#Name").val(),
"Description": $("#Description").val(),
"OrganizationID": $("#OrganizationID").val(),
"IsDeleted": false,
"RemainObligor": null,
"Editable": true,
"HasChildren": false,
//"Type": $("input[name=Type]").prop("checked", true).val(),
"IsActive": $("#IsActive").prop("checked"),
"BaseCurrencyId": $("#BaseCurrencyId").val(),
"AccountTypeId": $("#AccountTypeId").val(),
"BudgetCtrlTypeId": $("#BudgetCtrlTypeId").val(),
"ContraryToNatureCtrlId": $("#ContraryToNatureCtrlId").val(),
"NatureAccountId": $("#NatureAccountId").val()
};
$.ajax({
type: "PUT",
url: '/api/Account/Editt',
data: JSON.stringify(account),
contentType: "application/json;charset=utf-8",
processData: true,
success: function(data) {
alert(`'موفق'`);
},
error: function() {
alert('ناموفق');
}
});
});
</script>
[AcceptVerbs("POST", "Get")]
[System.Web.Http.HttpPost]
public void Edittt([FromBody]AccountGridViewModel a)
{
Account account=new Account();
account.AccountID = a.AccountID;
account.AccountTypeId = a.AccountTypeId;
account.Name = a.Name;
account.ParentID = a.ParentID;
account.NatureAccountId = a.NatureAccountId;
account.ContraryToNatureCtrlId = a.ContraryToNatureCtrlId;
account.BaseCurrencyId = a.BaseCurrencyId;
account.BudgetCtrlTypeId = a.BaseCurrencyId;
account.Description = a.Description;
account.IsActive = a.IsActive;
account.Organization = a.Organization;
account.OrganizationID = a.OrganizationID;
_accountService.UpdateAccount(account);
}
删除
[AcceptVerbs("POST", "Get")]
并更改
[System.Web.Http.HttpPost]
到
[System.Web.Http.HttpPut]
HttpPost 是 AcceptVerbs("POST") 的 shorthand。没有你在哪里告诉控制器接受 PUT。我不认为你可以让一个控制器方法接受多个动词,即使你可以我觉得一个更清晰的实现是有两个同名的方法,一个用 HttpGet 修饰,一个用 HttpPut 修饰。
我的 jQuery Put 请求不起作用,当我 运行 我的代码处于调试模式时,put 函数没有被调用,从 FireBug 我可以看到这个错误:
The requested resource does not support HTTP method 'PUT'
这是我的 jQuery 代码和 API 控制器:
<script>
$(document).ready(function() {
$('#success').hide();
function onSelect(e) {
$("#Type").prop("checked", false);
var name = $("#TreeView").data("kendoTreeView").dataItem(e.node).Name;
var accountId =$("#TreeView").data("kendoTreeView").dataItem(e.node).AccountId;
$("#AccountID").val(accountId).prop("disabled",true);
$("#Name").val(name).prop("disabled",true);
var type = $("#TreeView").data("kendoTreeView").dataItem(e.node).Nature;
$("#NatureAccountId").data("kendoDropDownList").value(type);
$("#AccountTypeId").data("kendoDropDownList").value(type);
$("#AccountTypeId").data("kendoDropDownList").enable(false);
$("#BaseCurrencyId").data("kendoDropDownList").value(type);
$("#ContraryToNatureCtrlId").data("kendoDropDownList").value(type);
$("#BudgetCtrlTypeId").data("kendoDropDownList").value(type);
var descrip = $("#TreeView").data("kendoTreeView").dataItem(e.node).Description;
$("#Description").val(descrip);
var idd = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentID;
$("#ParentID").val(idd);
var pName = $("#TreeView").data("kendoTreeView").dataItem(e.node).ParentName;
$("#ParentName").val(pName).prop("disabled", true);
var isActive = $("#TreeView").data("kendoTreeView").dataItem(e.node).IsActive;
$("#IsActive").val(isActive);
$("input:radio[name=Type][value='" + type + "']").prop("checked", true);
$("input[name=Type]").prop("disabled", true);
}
var accountchild = {
type: "odata",
transport: {
read: {
url: function(options) {
return kendo.format("/api/Account/GetAccountChilds?ParentID={0}&OrganizationID={1}", options.AccountId, @Session["OrganizationID"].ToString());
}
}
},
schema: {
model: {
id: "AccountId",
hasChildren: "HasChildren",
type: "Type",
name: "Name"
}
}
};
$("#TreeView").kendoTreeView({
dataSource: new kendo.data.HierarchicalDataSource({
type: "odata",
transport: {
read: {
url: "/api/Account/GetAllAccountParrentInfos?OrganizationID=@Session["OrganizationID"].ToString()",
dataType: "json"
}
},
schema: {
data: function(data) {
return data;
},
total: function(data) {
return data['odata.count'];
},
model: {
id: "AccountId",
hasChildren: "HasChildren",
children: accountchild,
type: "Nature",
name: "Name",
Description:"Description",
isActive:"IsActive",
parentName:"ParentName",
ParentID:"ParentID"
}
}
}),
select: onSelect,
dataTextField: ["DisplayName", "DisplayName"],
dataValueField: "AccountId"
});
});
</script>
<script>
$("#AccountTypeId").kendoDropDownList({
dataTextField: "Type",
dataValueField: "Id",
optionLabel: "انتخاب نوع حساب",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetAllAccounttypes"
}
}
}
});
$("#BaseCurrencyId").kendoDropDownList({
dataTextField: "Currency",
dataValueField: "Id",
optionLabel: "انتخاب ارز پایه",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetBaseCurrencies"
}
}
}
});
$("#NatureAccountId").kendoDropDownList({
dataTextField: "Nature",
dataValueField: "Id",
optionLabel: "انتخاب ماهیت حساب",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetNatureAccounts"
}
}
}
});
$("#ContraryToNatureCtrlId").kendoDropDownList({
dataTextField: "Ctrl",
dataValueField: "Id",
optionLabel: "کنترل خلاف ماهیت",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetContraryToNatureCtrls"
}
}
}
});
$("#BudgetCtrlTypeId").kendoDropDownList({
dataTextField: "CtrlType",
dataValueField: "Id",
optionLabel: "نوع کنترل بودجه",
dataSource: {
transport: {
read: {
dataType: "json",
url: "/api/Account/GetBudgetCtrlTypes"
}
}
}
});
</script>
<script>
$("#btn_edit").click(function() {
var account = {
"ParentID": $("#ParentID").val(),
"AccountID": $("#AccountID").val(),
"Name": $("#Name").val(),
"Description": $("#Description").val(),
"OrganizationID": $("#OrganizationID").val(),
"IsDeleted": false,
"RemainObligor": null,
"Editable": true,
"HasChildren": false,
//"Type": $("input[name=Type]").prop("checked", true).val(),
"IsActive": $("#IsActive").prop("checked"),
"BaseCurrencyId": $("#BaseCurrencyId").val(),
"AccountTypeId": $("#AccountTypeId").val(),
"BudgetCtrlTypeId": $("#BudgetCtrlTypeId").val(),
"ContraryToNatureCtrlId": $("#ContraryToNatureCtrlId").val(),
"NatureAccountId": $("#NatureAccountId").val()
};
$.ajax({
type: "PUT",
url: '/api/Account/Editt',
data: JSON.stringify(account),
contentType: "application/json;charset=utf-8",
processData: true,
success: function(data) {
alert(`'موفق'`);
},
error: function() {
alert('ناموفق');
}
});
});
</script>
[AcceptVerbs("POST", "Get")]
[System.Web.Http.HttpPost]
public void Edittt([FromBody]AccountGridViewModel a)
{
Account account=new Account();
account.AccountID = a.AccountID;
account.AccountTypeId = a.AccountTypeId;
account.Name = a.Name;
account.ParentID = a.ParentID;
account.NatureAccountId = a.NatureAccountId;
account.ContraryToNatureCtrlId = a.ContraryToNatureCtrlId;
account.BaseCurrencyId = a.BaseCurrencyId;
account.BudgetCtrlTypeId = a.BaseCurrencyId;
account.Description = a.Description;
account.IsActive = a.IsActive;
account.Organization = a.Organization;
account.OrganizationID = a.OrganizationID;
_accountService.UpdateAccount(account);
}
删除
[AcceptVerbs("POST", "Get")]
并更改
[System.Web.Http.HttpPost]
到
[System.Web.Http.HttpPut]
HttpPost 是 AcceptVerbs("POST") 的 shorthand。没有你在哪里告诉控制器接受 PUT。我不认为你可以让一个控制器方法接受多个动词,即使你可以我觉得一个更清晰的实现是有两个同名的方法,一个用 HttpGet 修饰,一个用 HttpPut 修饰。