Asp.net web Api with ODATA returns 406 Not Acceptable

Tarun Dhoddi 1 Reputation point
2021-01-19T07:44:42.123+00:00

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

Controller:

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();

        [Route("api/odata/SalesTools")]
        [HttpGet]
        [EnableQuery]
        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;
        }

        [EnableQuery]
        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)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

WebApiConfig:

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
            config.MapHttpAttributeRoutes();
            config.EnableDependencyInjection();
            var salesTools = new ODataConventionModelBuilder();
            salesTools.EntitySet<SalesTool>("SalesTools");
            config.MapODataServiceRoute("SalesTools", "odata", salesTools.GetEdmModel());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

        }
    }

Can anyone help me with this, please?

ASP.NET API
ASP.NET API
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.
348 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jerry Cai-MSFT 991 Reputation points
    2021-01-20T05:48:35.873+00:00

    Hi,tarun

    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:

    58386-xx.png
    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.