When I try to return an object using the odata method, I get the following error:
406 Not Acceptable
This is my code:
using System.Web.Http;
using APIDemo.Repositories;
using APIDemo.Data;
using Microsoft.AspNet.OData;
namespace APIDemo.Controllers
{
public class MyClass
{
public string Name { get; set; }
}
public class MyapitestController : ODataController
{
[HttpGet]
public IHttpActionResult Get()
{
var myclass = new MyClass()
{
Name = "Someone"
};
return Ok(myclass);
}
[HttpPut]
public IHttpActionResult Put([FromODataUri] string key, MyClass body)
{
return Ok();
}
[HttpPost]
public IHttpActionResult Post(MyClass test)
{
return Ok();
}
}
}
If I return a string in the Get() method, as follows: return Ok("some test"), this works fine. But when I try to return the class MyClass, it throws the error as you can see below.
You will have to check if you are using the same class in the configuration builder in webapiconfig.cs as you are using in your Controller.cs class.
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = "AirFlights";
builder.EntitySet<Person>("People");
return builder.GetEdmModel();
}
Controller:
using System.Web.Http;
using APIDemo.Repositories;
using APIDemo.Data;
using Microsoft.AspNet.OData;
namespace APIDemo.Controllers
{
public class Person
{
public string Name { get; set; }
}
public class PeopleController : ODataController
{
[HttpGet]
public IHttpActionResult Get()
{
var myclass = new Person()
{
Name = "Someone"
};
return Ok(myclass);
}
}
}
Or sometimes this occurs when referencing the wrong namespace, but in this case you are referencing the right ones:
using APIDemo.Data;
using Microsoft.AspNet.OData;