簡體   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