How to configure an OData endpoint in a self-service web API application

I am creating an OWIN standalone web API service that supports OWIN. I need this service to display OData endpoints.

The traditional method that supports IIS includes App_Start / WebApiConfig.cs:

using ProductService.Models; using System.Web.OData.Builder; using System.Web.OData.Extensions; public static class WebApiConfig { public static void Register(HttpConfiguration config) { // New code: ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Product>("Products"); config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: null, model: builder.GetEdmModel()); } } 

However, in my independent solution there is no such thing as WebApiConfig.cs

Where and how can I specify this OData configuration?

+5
source share
1 answer

You are right, not necessarily such a thing as WebApiConfig.cs in a self-service OWIN project, because you are declaring the middleware that you need when you need it. However, if you are following OWIN's self-organizing tutorials, you probably came across the Startup.cs concept, which you can use as you can create an HttpConfiguration here.

 public class Startup { public void Configuration(IAppBuilder appBuilder) { // Configure Web API for self-host. HttpConfiguration config = new HttpConfiguration(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.EntitySet<Product>("Products"); config.MapODataServiceRoute( routeName: "ODataRoute", routePrefix: null, model: builder.GetEdmModel()); appBuilder.UseWebApi(config); } } 
+10
source

All Articles