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?
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.
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.
MVC controllers inherited from Controller ; Web API controllers inherited from ApiController .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With