I am trying to implement OData routing in my ASP.NET web api. For guidance, I looked at this tutorial: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint .
However, I keep on getting an error message in the MapODataServiceRoute() function. Apparently, the function is expecting a Microsoft.OData.Edm.IEdmModel, and my builder's GetEdmModel() function only returns Microsoft.Data.Edm.IEdmModel.
I did some research online. Microsoft.Data.Edm is a library for the older version of OData. Microsoft.OData.Edm is for OData v4.0, which is why I commented out Microsoft.Data.Edm in 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 (i.e. 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 am still getting an 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 a Microsoft.OData.Edm.IEdmModel? Or am I going to have to just do a cast?
Replacing System.Web.Http.OData.Builder with System.Web.OData.Builder seems to work.
Here is the link with the explanation: https://devblogs.microsoft.com/aspnet/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0/
I think this line pretty much sums it up though:
The assembly name and the root namespace are now System.Web.OData instead of System.Web.Http.OData.
The headers I am using are now:
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;
Please use
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
instead of
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
i wish this will help, i got same error when i tried to do same example, i just did the following by package manager console
1- unInstall-Package Microsoft.OData.Edm -Force 2- Install-Package Microsoft.OData.Edm
then it works fine with me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.