如何实现最小控制器
How to Implement Minimal Controller
我有一个 ASP.NET Core 1.1 Web 项目。
我已经安装了Microsoft.OData.Core and followed linked "Getting Started" under http://odata.github.io/。
该页面上的以下两个链接均适用于 .Net 45
- “Build an OData v4 Service with RESTier Library”
- “Build an OData v4 Service with OData WebApi Library”
本月 SO answer links to Microsoft.AspNetCore.OData 不属于 Microsoft,上次更新是在一年前。
这意味着"OData Support in ASP.net core"
我看到这个第三方解决方案 AutoODataEF.Core 但是可以自动生成控制器。
最后,我看到这个 git issue 表示 ASP.Net 核心的 OData WebAPI 即将推出,但目前最终不可用。
假设我有一个 Person 模型和一个 EF DbContext。
如何实现最小的 OData 控制器?
odata on asp.net core netcoreapp2.0, 20180216
安装包Microsoft.AspNetCore.OData-Pre {7.0.0-beta1}
在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); // e.g. http://localhost:port/data/someclass?...
// insert special bits for e.g. custom MLE here
routeBuilder.EnableDependencyInjection();
routeBuilder.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); // enable mvc controllers
});
}
在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;
}
}
我有一个 ASP.NET Core 1.1 Web 项目。
我已经安装了Microsoft.OData.Core and followed linked "Getting Started" under http://odata.github.io/。
该页面上的以下两个链接均适用于 .Net 45
- “Build an OData v4 Service with RESTier Library”
- “Build an OData v4 Service with OData WebApi Library”
本月 SO answer links to Microsoft.AspNetCore.OData 不属于 Microsoft,上次更新是在一年前。
这
我看到这个第三方解决方案 AutoODataEF.Core 但是可以自动生成控制器。
最后,我看到这个 git issue 表示 ASP.Net 核心的 OData WebAPI 即将推出,但目前最终不可用。
假设我有一个 Person 模型和一个 EF DbContext。
如何实现最小的 OData 控制器?
odata on asp.net core netcoreapp2.0, 20180216
安装包Microsoft.AspNetCore.OData-Pre {7.0.0-beta1}
在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); // e.g. http://localhost:port/data/someclass?... // insert special bits for e.g. custom MLE here routeBuilder.EnableDependencyInjection(); routeBuilder.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"); // enable mvc controllers }); }
在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; } }