Javascript AcceptVerbs在Web API控制器中不工作

Javascript AcceptVerbs在Web API控制器中不工作,javascript,asp.net-mvc-4,asp.net-web-api,Javascript,Asp.net Mvc 4,Asp.net Web Api,我试图通过一个ajax请求在WebAPI控制器中调用一个动作,如下所示 $.ajax({ url: "/api/GuestPartyAPI/" + cerid, type: "GETAD", async: false, data: { Id: id, Name: name, NeedsHotel: needshotel_bool, TableNo: tableno, QR_CodeImage: qrc

我试图通过一个ajax请求在WebAPI控制器中调用一个动作,如下所示

$.ajax({
            url: "/api/GuestPartyAPI/" + cerid,
            type: "GETAD",
            async: false,
            data: { Id: id, Name: name, NeedsHotel: needshotel_bool, TableNo: tableno, QR_CodeImage: qrcodeimage,
                AddressLabel: addresslabel, Address_1: address1, Address_2: address2, City: city, State: state,
                PostalCode: postalcode, Country: country, Email: email, Phone: phone
            },
            dataType: "json" 
        }).fail(function (jqXHR, textStatus) {
            console.log(jqXHR);
            console.log(textStatus);
            //alert("cerid " + cerid);
            //alert("Request failed: " + textStatus);
        });
[AcceptVerbs("GETAD")]
    public HttpResponseMessage GetGuestPartyCer(int cerid, GuestParty guestparty) 
    {

        if (ModelState.IsValid)
        {
            db.GuestParties.AddObject(guestparty);
            db.SaveChanges();

            CeremonyGuestParty ceremonygp = new CeremonyGuestParty(); //create a CeremonyGuestParty entry to link ceremony and guestparty
            ceremonygp.CeremonyId = cerid;
            ceremonygp.GuestPartyId = guestparty.Id;
            db.CeremonyGuestParties.AddObject(ceremonygp);
            db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, guestparty);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = guestparty.Id }));
            return response;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
控制器中的操作如下所示

$.ajax({
            url: "/api/GuestPartyAPI/" + cerid,
            type: "GETAD",
            async: false,
            data: { Id: id, Name: name, NeedsHotel: needshotel_bool, TableNo: tableno, QR_CodeImage: qrcodeimage,
                AddressLabel: addresslabel, Address_1: address1, Address_2: address2, City: city, State: state,
                PostalCode: postalcode, Country: country, Email: email, Phone: phone
            },
            dataType: "json" 
        }).fail(function (jqXHR, textStatus) {
            console.log(jqXHR);
            console.log(textStatus);
            //alert("cerid " + cerid);
            //alert("Request failed: " + textStatus);
        });
[AcceptVerbs("GETAD")]
    public HttpResponseMessage GetGuestPartyCer(int cerid, GuestParty guestparty) 
    {

        if (ModelState.IsValid)
        {
            db.GuestParties.AddObject(guestparty);
            db.SaveChanges();

            CeremonyGuestParty ceremonygp = new CeremonyGuestParty(); //create a CeremonyGuestParty entry to link ceremony and guestparty
            ceremonygp.CeremonyId = cerid;
            ceremonygp.GuestPartyId = guestparty.Id;
            db.CeremonyGuestParties.AddObject(ceremonygp);
            db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, guestparty);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = guestparty.Id }));
            return response;
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
同一控制器中的类似操作是通过类似的ajax请求来处理[AcceptVerbs(“GETAC”)],但不是通过将动词“GETAD”更改为其他内容

这是密码 $.ajax({ url:“/api/GuestPartyAPI/”+id, 类型:“GETAC”, async:false,
数据类型:“json”})

您的问题与使用的
AcceptVerbs
无关,但路由和动作参数匹配是如何工作的:

如果动作参数来自路由参数,则参数名称必须与您在路由中定义的参数名称相匹配

因此,如果使用APIController的默认路由:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
注意,您的route参数被称为
{id}

因此,在您的操作中,您需要使用相同的名称
id
,以便框架能够进行匹配

只需将您的动作签名
cerid
更改为
id
,它就可以工作了:

public HttpResponseMessage GetGuestPartyCer(int id, GuestParty guestparty)

你能发布工作的
$.ajax
调用的代码吗?这里是代码$.ajax({url://api/GuestPartyAPI/“+id,type:“GETAC”,async:false,dataType:“json”});你使用哪种浏览器?您的WebApiConfig路由配置看起来如何?您可能需要将操作更改为
GetGuestPartyCer(int-id,GuestParty-GuestParty)
注意
int-id
以匹配默认路由。通过更改为“int-id”,它就起作用了。谢谢你。