How to implement a minimal controller

I have an ASP.NET Core 1.1 web project.

I installed Microsoft.OData.Core and followed the Getting Started link at http://odata.imtqy.com/ .

Both of the following links on this page are for .Net 45

This month, SO respond to Microsoft.AspNetCore.OData , which is NOT Microsoft owned and last updated more than a year ago.

This SO answer implies "Support for OData in the ASP.net Core"

I see this third-party AutoODataEF.Core solution for automatically creating controllers.

Finally, I see that the git issue indicates that OData WebAPI for ASP.Net Core is coming out, but it is ultimately not available at the moment.

Assuming I have a Person model and an EF DbContext.

How to implement a minimal OData controller?

+7
odata asp.net-core restier
source share
1 answer

odata on asp.net core netcoreapp2.0, 20180216

  • install-package Microsoft.AspNetCore.OData -Pre {7.0.0-beta1}

  • in Startup.cs:

    public virtual void ConfigureServices(IServiceCollection services) { // ... services.AddMvc(); // mvc first services.AddOData(); // odata second } public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // ... var builder = new ODataConventionModelBuilder(serviceProvider); builder.EntitySet<SomeClass>(nameof(SomeClass).ToLower()).EntityType.HasKey(s => s.SomeId); builder.EntitySet<OtherClass>(nameof(OtherClass).ToLower()).EntityType.HasKey(s => s.OtherId).MediaType(); // etc var model = builder.GetEdmModel(); app.UseMvc(routeBuilder => { routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(null).Count(); routeBuilder.MapODataServiceRoute("ODataRoute", "data", model); // eg http://localhost:port/data/someclass?... // insert special bits for eg custom MLE here routeBuilder.EnableDependencyInjection(); routeBuilder.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); // enable mvc controllers }); } 
  • in SomeClassController.cs:

     public class SomeClassController : ODataController // or just plain Controller { [EnableQuery] [HttpGet] [ODataRoute("someclass")] public List<SomeClass> Get() // this should maybe be an IQueryable wrapped by an IActionResult/OkObjectResult { List<SomeClass> list = new List<SomeClass>(); // however you do this return list; } } 
+1
source share

All Articles