web Api with ODATA returns 406 Not Acceptable

Tarun Dhoddi 1 Reputation point

I am trying to implement a web API endpoint that returns ODATA. But when I am trying to hit the endpoint the code in the method was executing successfully but getting 406 Not Acceptable error. Below is my code


using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNet.OData.Query;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

    public class SalesToolsController : ODataController
        DBSQLDataContext db = new DBSQLDataContext();

        public IEnumerable<SalesTool> Get(ODataQueryOptions options)
            var documents = db.SalesTools;
        var countQuery = options.Filter.ApplyTo(documents, new ODataQuerySettings()) as IQueryable<SalesTool>;
        var count = countQuery.Count();
        return documents;

        public SingleResult<SalesTool> Get([FromODataUri] int key)
            IQueryable<SalesTool> result = db.SalesTools.Where(p => p.Id == key);
            return SingleResult.Create(result);

        protected override void Dispose(bool disposing)


using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using System.Web.Http;
public static class WebApiConfig
        public static void Register(HttpConfiguration config)
            // Web API configuration and services

            // Web API routes
            var salesTools = new ODataConventionModelBuilder();
            config.MapODataServiceRoute("SalesTools", "odata", salesTools.GetEdmModel());

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }


Can anyone help me with this, please?

ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
306 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jerry Cai-MSFT 986 Reputation points


    406 error often indicates the server cannot produce a response matching the list of acceptable values.

    You can check the F12->Network->Request Header->accept, check whether it can accept the type of your passing data.

    public class SalesToolsController : ODataBaseController

    What is your ODataBaseController? Do you have some custom in it, I changed it to ODataController and used your shared code, everything

    works well, so can you share this class?

    And this is my demo result, query id and name when key=1:

    Best Regards,
    Jerry Cai

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.