WebAPI PUT and POST go to 404
本文关键字:to go POST PUT and WebAPI | 更新日期: 2023-09-27 18:29:44
我正试图使用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)
{
}
公司概览查看:
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)
}
我得到了一个404错误,尽管使用了默认的WebAPI路由。为什么这不会挂钩?我是不是错过了什么?
尝试将processData: false
和contentType: 'application/json; charset=utf-8'
添加到ajaxProperties
中。这将告诉ajax
将请求作为JSON主体而不是URL参数发送。
这可能是你在不需要的时候在URL中添加Id。试着在ajax中发布/放置到/api/company。或者,只需尝试将"string id"作为Put方法中的另一个参数,看看这是否有帮助。
不过,在这种情况下,您可能还需要将FromBody或FromUri属性放在每个参数上。