Showing posts with label v4. Show all posts
Showing posts with label v4. Show all posts

Thursday, February 5, 2015

WebAPI OData v4: Cannot serialize a null 'entry'

In that kind of function:

public IHttpActionResult MyFunction([FromODataUri]int myParameter)
{
 return Ok(GetFoo(myParameter));
}

This may lead to the error 500 described in the title if the returned value is null. Based on this thread, One way would be to return an empty response when the return value is null:

public IHttpActionResult MyFunction([FromODataUri]int myParameter)
{
 var retval = GetFoo(myParameter);
 if (retval != null)
  return Ok(retval);
 else
  return Ok();
}

The result is a 202 OK empty response, but this doesn't seem to please some OData frameworks as I sometimes get a "Unexpected server response" message on the client side.

Now, based on this other thread, it seems the clean way to do this is to return a 404 error when the return value is null and act accordingly on the client side:

public IHttpActionResult MyFunction([FromODataUri]int myParameter)
{
 var retval = GetFoo(myParameter);
 if (retval != null)
  return Ok(retval);
 else
  return NotFound();
}


Wednesday, December 3, 2014

ASP.Net: OData V4 Controller giving 406

What an obscure one...

If you have a seemingly complete controller and opening this url:

http://yoursite/api/Products

returns actual OData v4 results BUT this:

http://yoursite/api/Products(1)

returns a 404 error with:

No type was found that matches the controller named 'Products(1)'

then make sure your "using"s are adequate in your controller:

using System.Web.OData; <-- this should be used

vs

using System.Web.Http.OData;

By default, the WebAPI 2 ODataController template generates a class with the latter, and it gave me trouble.