简体   繁体   中英

"The requested resource does not support HTTP method 'PUT' ASP.Net Web API

My jQuery Put request does not work, when I run my code in debug mode the put function does not get called, and from FireBug I can see this error:

The requested resource does not support HTTP method 'PUT'

Here is my jQuery code and API controller:

<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);
}

remove the

[AcceptVerbs("POST", "Get")]

and change the

[System.Web.Http.HttpPost]

to

[System.Web.Http.HttpPut]

HttpPost is a shorthand for AcceptVerbs("POST"). No where are you telling the controller to accept PUT. I don't think you can have a single controller method accept multiple verbs, even if you can I feel like a cleaner implementation would be to have two methods with the same name one decorated with HttpGet and one decorated with HttpPut.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM