Error enabling OData v4.0 routing in ASP.NET Web API 2.2

I am trying to implement OData routing in ASP.NET ASP.NET. For guidance, I looked through this tutorial: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint .

However, I continue to receive an error message in the MapODataServiceRoute () function. Apparently the function expects Microsoft.OData.Edm.IEdmModel, and my GetEdmModel () function of my builder returns Microsoft.Data.Edm.IEdmModel.

I have done some research online. Microsoft.Data.Edm is a library for an older version of OData. Microsoft.OData.Edm for OData v4.0, so I commented on the Microsoft.Data.Edm file in the WebApiConfig.cs file. Here is my code.

using MyApp.Models; // using Microsoft.Data.Edm; using Microsoft.OData.Edm; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http.Headers; using System.Web.Http; using System.Web.Http.OData.Builder; using System.Web.OData.Extensions; using System.Web.OData.Routing; namespace MyAppAPI { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Enable attribute routing config.MapHttpAttributeRoutes(); // Enable OData routing config.MapODataServiceRoute( routeName: "MyApp", routePrefix: "odata", model: GetEdmModel()); // Conventional routing config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type. // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries. // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712. config.EnableQuerySupport(); // To disable tracing in your application, please comment out or remove the following line of code // For more information, refer to: http://www.asp.net/web-api config.EnableSystemDiagnosticsTracing(); // Trying to get most browsers (ie Google Chrome) to return JSON config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); } // Configure modesl to use Odata public static IEdmModel GetEdmModel() { ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<ModelA>("ModelA"); builder.EntitySet<ModelB>("ModelB"); return builder.GetEdmModel(); } } } 

However, I still get the error message:

 Error 1 Cannot implicitly convert type 'Microsoft.Data.Edm.IEdmModel' to 'Microsoft.OData.Edm.IEdmModel'. An explicit conversion exists (are you missing a cast?) 

Is there a clean way to get the Microsoft.OData.Edm.IEdmModel file? Or do I just need to do the cast?

+5
source share
2 answers

Replaces System.Web.Http.OData.Builder with System.Web.OData.Builder seems to work.

Here is an explanation link: http://blogs.msdn.com/b/webdev/archive/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4 -0.aspx

I think this line pretty much sums it up:

Now the assembly name and root namespace is System.Web.OData instead of System.Web.Http.OData.

The headers I'm using now are:

 using MyApp.Models; // using Microsoft.Data.Edm; using Microsoft.OData.Edm; using System.Net.Http.Headers; using System.Web.Http; // using System.Web.Http.OData.Builder; using System.Web.OData.Builder; using System.Web.OData.Extensions; 
+9
source

I want this to help, I got the same error when I tried to make the same example, I just did the following on the package manager console

1- unInstall-Package Microsoft.OData.Edm -Force 2- Install-Package Microsoft.OData.Edm

then it works great with me.

+1
source

Source: https://habr.com/ru/post/1216125/


All Articles