WebAPI and ODataController return 406 Not Acceptable

Ivan-Mark Debono picture Ivan-Mark Debono · Oct 31, 2014 · Viewed 37.4k times · Source

Before adding OData to my project, my routes where set up like this:

       config.Routes.MapHttpRoute(
            name: "ApiById",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" },
            handler: sessionHandler
        );

        config.Routes.MapHttpRoute(
            name: "ApiByAction",
            routeTemplate: "api/{controller}/{action}",
            defaults: new { action = "Get" },
            constraints: null,
            handler: sessionHandler
        );

        config.Routes.MapHttpRoute(
            name: "ApiByIdAction",
            routeTemplate: "api/{controller}/{id}/{action}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"^[0-9]+$" },
            handler: sessionHandler

All controllers provide Get, Put (action name is Create), Patch (action name is Update) and Delete. As an example, the client uses these various standard url's for the CustomerType requests:

string getUrl =  "api/CustomerType/{0}";
string findUrl = "api/CustomerType/Find?param={0}";
string createUrl = "api/CustomerType/Create";
string updateUrl = "api/CustomerType/Update";
string deleteUrl = "api/CustomerType/{0}/Delete";

Then I added an OData controller with the same action names as my other Api controllers. I also added a new route:

        ODataConfig odataConfig = new ODataConfig();

        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: odataConfig.GetEdmModel()
        );

So far I changed nothing on the client side. When I send a request, I get a 406 Not Available error.

Are the routes getting mixed up? How can I solve this?

Answer

JeeShen Lee picture JeeShen Lee · Jun 16, 2015

If you are using OData V4, replace using System.Web.Http.OData;

With using Microsoft.AspNet.OData; (Please check the comments for the latest library)

in the ODataController works for me.