繁体   English   中英

WebAPI PUT和POST转到404

[英]WebAPI PUT and POST go to 404

我正在尝试使用AJAX将该对象放置到/ api / company /#/中:

    {"CompanyID":2,"Name":"Test Company","Address1":"","Address2":"","City":"","State":"","Zip":"","ContactName":"","ContactPhone":"","ContactEmail":"","EmployeeCount":"","TypeOfIndustry":"","CompanyRevenue":""}

我的PUT方法:

    public void Put (CompanyOverviewView company)
    {
    }

CompanyOverviewView:

public class CompanyOverviewView {
    public int CompanyID { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string ContactName { get; set; }
    public string ContactPhone { get; set; }
    public string ContactEmail { get; set; }
    public string CompanyType { get; set; }
    public Nullable<int> EmployeeCount { get; set; }
    public string TypeOfIndustry { get; set; }
    public Nullable<decimal> CompanyRevenue { get; set; }

AJAX属性:

var ajaxProperties = {
        type: "PUT",
    url: "/api/company/5/",
    dataType: "json",
    data: JSON.stringify(postdata)
    }

尽管使用默认的WebAPI路由,但出现404错误。 为什么不挂起来呢? 我想念什么吗?

尝试添加processData: falsecontentType: 'application/json; charset=utf-8' contentType: 'application/json; charset=utf-8'到您的ajaxProperties 这将告诉ajax将请求作为JSON正文而不是URL参数发送。

可能是您在不需要时在URL中添加了ID。 尝试在ajax中仅将发布/放到/ api / company。 或者,您也可以在Put方法中尝试将“字符串ID”作为另一个参数,看看是否有帮助。

在这种情况下,您可能还需要将FromBody或FromUri属性放在每个参数上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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