简体   繁体   English

WebAPI PUT和POST转到404

[英]WebAPI PUT and POST go to 404

I'm trying to use AJAX to PUT this object to the /api/company/#/: 我正在尝试使用AJAX将该对象放置到/ api / company /#/中:

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

My PUT method: 我的PUT方法:

    public void Put (CompanyOverviewView company)
    {
    }

CompanyOverviewView: 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 Properties: AJAX属性:

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

I am getting a 404 error, though using the default WebAPI routing. 尽管使用默认的WebAPI路由,但出现404错误。 Why would this not hook up? 为什么不挂起来呢? Am I missing something? 我想念什么吗?

Try adding processData: false and contentType: 'application/json; charset=utf-8' 尝试添加processData: falsecontentType: 'application/json; charset=utf-8' contentType: 'application/json; charset=utf-8' to your ajaxProperties . contentType: 'application/json; charset=utf-8'到您的ajaxProperties This will tell ajax to send the request as a JSON body instead of URL parameters. 这将告诉ajax将请求作为JSON正文而不是URL参数发送。

It could be that you are adding the Id in the URL when you don't need to. 可能是您在不需要时在URL中添加了ID。 Try posting/putting to just /api/company in the ajax. 尝试在ajax中仅将发布/放到/ api / company。 Or alternatively just try 'string id' as another param in your Put method and see if that helps. 或者,您也可以在Put方法中尝试将“字符串ID”作为另一个参数,看看是否有帮助。

You also might need to put FromBody or FromUri Attributes on the each of the arguments in this case though. 在这种情况下,您可能还需要将FromBody或FromUri属性放在每个参数上。

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

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