Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api call is returning a 404 error with GUID passed as param

I am having troubles with my ajax call making it to my web.api method. If I remove the Guid orderId from both the api and the js, the call makes it to the controller, but the pizza object is empty. If I pass the Guid in the URL, it also makes it to the controller but no pizza. Please explain why this doesn't work or help me make it work.

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);
};

Web Api:

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

JS Objects:

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;
};

C# Objects:

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; }
}

JSON Fiddler Post Capture:

enter image description hereenter image description here

like image 359
Boone Avatar asked Aug 27 '13 22:08

Boone


People also ask

How do I fix error 404 in Web API?

A simple solution is to check for the HTTP status code 404 in the response. If found, you can redirect the control to a page that exists. The following code snippet illustrates how you can write the necessary code in the Configure method of the Startup class to redirect to the home page if a 404 error has occurred.

What can someone say if your API returns 404?

HTTP 404 is more traditionally associated with a bad URI. So in effect we're saying "Either you got to the right place, but that specific record does not exist, or there's no such location on the Internets!

When should I return API 404?

Use 404 when resource is none of consumer's business (and never will) The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. Returning a 404 Not Found means “the requested resource does not exists”.


2 Answers

In my experience the WEB API 2 HttpPost does not work if I try to put a Guid in the parameter collection. I have to put the Guid into the URI, e.g.

  var url = window.applicationBaseUrl + "aiapps/"  
             + GuidOrderId + "/DeleteByGroupId";

And then the ApiController like this

    [HttpPost]
    [Route("~/aiapps/{guid:Guid}/deletebygroupid")]
    public AiAppsOpResult DeleteByGroupId(Guid guid)
    {         

Some have suggested that it will work to use a JSON object for the Guid that has an empty key, e.g. {'':OrderId} but not for me. And it does no good to declare the Guid parameter optional or nullable in the controller. I have not tested @Badri's solution, but I think it will work. I do know the WEB API will work if I make a C# DTO that contains both the Pizza and the OrderIdGuid and POST that, e.g.

    public class MetaPizza 
    {
         public Pizza pizza {get;set;} 
         public Guid OrderId{get;set;}
    }

and

    [Route("~/api/postorder")]
    [HttpPost]
    public AiAppsOpResult CalcRisk(MetaPizza metaPizza)



$.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: window.applicationBaseUrl + "api/postorder" ,
        data: _JSON_MetaPizza
    })
like image 25
subsci Avatar answered Sep 28 '22 08:09

subsci


ASP.NET Web API, by default, binds request body to a complex type (Pizza, in your case). Web API binds the body in its entirety to one parameter. Simple types such as GUID are bound from the URI path and query string. So, by passing GUID in the URI and posting only the JSON corresponding to pizza object (only the pizza and not anything else such as order ID), you can get this working.

like image 53
Badri Avatar answered Sep 28 '22 09:09

Badri