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?
A few weeks ago we started publishing nightly builds for our initial support in ASP.NET Web API for the OData v4.0 protocol. Our OData v4.0 support is based on the OData Library for OData v4.0 that has been released in the past few months.
This article describes the routing conventions that Web API 2 in ASP.NET 4.x uses for OData endpoints. When Web API gets an OData request, it maps the request to a controller name and an action name. The mapping is based on the HTTP method and the URI.
Use ASP.NET Web API to create endpoints that support OData Version 4. The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations... Tutorial with steps and visuals on how to create a client for the CRUD operations OData service.
OData is a data access protocol for the web. It provides a uniform way to query and manipulate data sets. Web API supports both Version 3 and Version 4 of the OData protocol. Use ASP.NET Web API to create endpoints that support OData Version 4. The Open Data Protocol (OData) is a data access protocol for the web.
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;
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