Web Api调用返回404错误,GUID作为参数传递

Boo*_*one 5 asp.net-mvc jquery asp.net-mvc-4 asp.net-web-api

我的ajax调用让我的web.api方法变得麻烦.如果我从api和js中删除了Guid orderId,则调用将其发送给控制器,但是pizza对象为空.如果我在URL中传递Guid,它也会转到控制器但没有披萨.请解释为什么这不起作用或帮助我使其工作.

JS:

var savePizza = function (orderId, pizza) {
    var dataCall = $.ajax(config.savePizzaUrl, {
        data: ko.toJSON({ orderId: orderId, pizza: pizza }),
        type: "post",
        contentType: "application/json"
    });

    return Q.when(dataCall);
};
Run Code Online (Sandbox Code Playgroud)

Web Api:

    [HttpPost]
    public RequestReturn<Guid> SavePizza(Guid orderId, Pizza pizza)
    {
        return PizzaRequests.SavePizza(orderId, pizza);
    }
Run Code Online (Sandbox Code Playgroud)

JS对象:

var pizza = function (data) {
    this.Id = data.Id;
    this.Size = new size(data.Size);
    this.SizeId = data.SizeId;
    this.Toppings = $.map(data.Toppings, function(item) {return new topping(item);});
};
var topping = function (data) {
    this.Id = data.Id;
    this.Name = data.Name;
    this.Price = data.Price;
};
var size = function (data) {
    this.Id = data.Id;
    this.Name = data.Name;
    this.Price = data.Price;
};
Run Code Online (Sandbox Code Playgroud)

C#对象:

public class Pizza
{
    public Guid Id { get; set; }
    public Guid SizeId { get; set; }
    public Size Size { get; set; }
    public IEnumerable<Topping> Toppings { get; set; }
}
public class Size
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}
public class Topping
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

JSON Fiddler Post Capture:

在此输入图像描述 在此输入图像描述

Bad*_*dri 9

默认情况下,ASP.NET Web API将请求主体绑定到复杂类型(在您的情况下为Pizza).网页API结合身体的全部,以一个参数.诸如GUID的简单类型从URI路径和查询字符串绑定.因此,通过在URI中传递GUID并仅发布与披萨对象相对应的JSON(仅披萨而不是订单ID等其他任何东西),您可以使其正常工作.