WebAPI PUT和POST转到404

时间:2013-08-29 19:22:29

标签: c# asp.net ajax asp.net-mvc asp.net-web-api

我正在尝试使用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错误。为什么这不会挂钩?我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

可能是您在不需要时在URL中添加ID。尝试在ajax中发布/放入/ api / company。或者只是尝试将'string id'作为Put方法中的另一个参数,看看是否有帮助。

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