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?