I've looked at all the SO
posts on this and had no success. I'm able to call into the OData
controller and see the data retrieved, mapped, and packaged-up. The controller method exits and I get a 406 - Not Acceptable
. I'm only using the System.Web.OData
namespace (OData Version v4.0.30319) and, using Postman
, I manually set the Content
and Accept
headers to 'application/json' with no luck
Maybe I've missed something over the last 2 hours reading everyone's posts on similar problems. Any pointers will be appreciated.
UPDATE:
The problem appears to be in the Mapper code (Automapper
) as pointed out by Igor below. It looks like there's a commitment to return the database(EF) entity, not a mapped class. With this knowledge I found this SO post, but it doesn't offer a solution: ApiController vs ODataController when exposing DTOs. Are we stuck having to return database entities or can the results be mapped? If so, that's a deal breaker for me.
Controller:
[EnableQuery]
[HttpGet]
public async Task<IHttpActionResult> Get()
{
var list = await db.ConfigSets.ToListAsync();
Mapper.CreateMap<ConfigSet, ConfigSetDTO>();
var configSetDTOs = Mapper.Map<List<ConfigSet>, List<ConfigSetDTO>>(list);
return Ok(configSetDTOs); //IT LOOKS GOOD HERE!
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.EnableCors();
// OData - must be before config.Routes when using a prefix. In this case "api"
config.MapODataServiceRoute("odata", "api", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.Namespace = "Services";
builder.ContainerName = "DefaultContainer";
builder.EntitySet<ConfigSet>("Configuration");
var edmModel = builder.GetEdmModel();
return edmModel;
}
}