Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI and ODataController return 406 Not Acceptable

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?

like image 856
Ivan-Mark Debono Avatar asked Oct 31 '14 14:10

Ivan-Mark Debono


People also ask

Can we return view from WebAPI?

You can return one or the other, not both. Frankly, a WebAPI controller returns nothing but data, never a view page. A MVC controller returns view pages. Yes, your MVC code can be a consumer of a WebAPI, but not the other way around.

Can we use ActionResult in Web API?

ASP.NET Core includes the ActionResult<T> return type for web API controller actions. It enables you to return a type deriving from ActionResult or return a specific type. ActionResult<T> offers the following benefits over the IActionResult type: The [ProducesResponseType] attribute's Type property can be excluded.

What is the base controller for all WebAPI controllers to inherit from?

MVC controllers inherited from Controller ; Web API controllers inherited from ApiController .


1 Answers

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.

like image 97
JeeShen Lee Avatar answered Sep 21 '22 02:09

JeeShen Lee