C# ASP.Net核心&x2B;EF+;ODataV4核心测试版2
在ASP.NET核心上有一个相当基本的Web API项目,配置了EF,该项目可以使用Web API正常工作。我正在跟随文章转换为使用Odata,但我不能完全让它工作 我有一个名为customer的父对象,它有两个子对象:Addresses和Person。我已经为数据库设定种子,以便可以看到那里有数据,Odata端点看起来不错,因为当您启动项目时,它会显示实体,Odata/$metadata会按预期显示EDM结构 我目前唯一的问题是,当我导航到一个URL(如/odata/customers)时,我会收到一个空白屏幕。邮递员返回404 我已经梳理了Lucas的示例项目(我正在关注的那篇文章),并在网上进行了大量研究,不太明白我做错了什么 我相信这是件简单/愚蠢的事情,但欢迎任何建设性的指导:) **编辑**为简单起见删除了其他代码(并基于反馈)。如果需要更多信息,请告诉我 干杯 亚当 文件路径:Odata\BookingsModelBuilder.csC# ASP.Net核心&x2B;EF+;ODataV4核心测试版2,c#,entity-framework,odata,asp.net-core-webapi,C#,Entity Framework,Odata,Asp.net Core Webapi,在ASP.NET核心上有一个相当基本的Web API项目,配置了EF,该项目可以使用Web API正常工作。我正在跟随文章转换为使用Odata,但我不能完全让它工作 我有一个名为customer的父对象,它有两个子对象:Addresses和Person。我已经为数据库设定种子,以便可以看到那里有数据,Odata端点看起来不错,因为当您启动项目时,它会显示实体,Odata/$metadata会按预期显示EDM结构 我目前唯一的问题是,当我导航到一个URL(如/odata/customers)时,我
using System;
using Microsoft.AspNet.OData.Builder;
using Microsoft.OData.Edm;
using Bookings_Server.OData.Models;
namespace Bookings_Server
{
public class BookingsModelBuilder
{
public IEdmModel GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
builder.EntitySet<Address>("addresses")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
builder.EntitySet<Customer>("customers")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
builder.EntitySet<Person>("people")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select();// Allow for the $select Command;
return builder.GetEdmModel();
}
}
}
使用系统;
使用Microsoft.AspNet.OData.Builder;
使用Microsoft.OData.Edm;
使用Bookings_Server.OData.Models;
命名空间预订服务器
{
公共类BookingsModelBuilder
{
公共IEdmModel GetedModel(IServiceProvider服务提供程序)
{
var builder=新ODataConventionModelBuilder(服务提供商);
builder.EntitySet(“地址”)
.EntityType
.Filter()//允许使用$Filter命令
.Count()//允许使用$Count命令
.Expand()//允许使用$Expand命令
.OrderBy()//允许使用$OrderBy命令
.Page()//允许使用$top和$skip命令
.Select();//允许使用$Select命令;
builder.EntitySet(“客户”)
.EntityType
.Filter()//允许使用$Filter命令
.Count()//允许使用$Count命令
.Expand()//允许使用$Expand命令
.OrderBy()//允许使用$OrderBy命令
.Page()//允许使用$top和$skip命令
.Select();//允许使用$Select命令;
builder.EntitySet(“人”)
.EntityType
.Filter()//允许使用$Filter命令
.Count()//允许使用$Count命令
.Expand()//允许使用$Expand命令
.OrderBy()//允许使用$OrderBy命令
.Page()//允许使用$top和$skip命令
.Select();//允许使用$Select命令;
返回builder.GetEdmModel();
}
}
}
文件路径:EF\DataContext.CS
using Microsoft.EntityFrameworkCore;
using Bookings_Server.OData.Models;
namespace Bookings_Server.EF
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<Tenant> Tenants { get; set; }
}
}
使用Microsoft.EntityFrameworkCore;
使用Bookings_Server.OData.Models;
命名空间Bookings_Server.EF
{
公共类DataContext:DbContext
{
公共DataContext(DbContextOptions选项):基本(选项){}
公共数据库集地址{get;set;}
公共数据库集客户{get;set;}
公共数据库集人物{get;set;}
公共数据库集租户{get;set;}
}
}
文件路径:Controllers\CustomersController.cs
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Bookings_Server.EF;
using Bookings_Server.OData.Models;
using Microsoft.AspNet.OData;
namespace Bookings_Server.OData.Controllers
{
[Produces("application/json")]
public class CustomersController : ODataController
{
private readonly DataContext _context;
public CustomersController(DataContext context)
{
_context = context;
}
// GET: odata/customers
[EnableQuery(PageSize = 20)]
public IQueryable<Customer> Get() => _context.Customers.AsQueryable();
/*
public IActionResult Get()
{
return Ok(_context.Customers.AsQueryable());
}
*/
}
}
使用System.Linq;
使用Microsoft.AspNetCore.Mvc;
使用Bookings_Server.EF;
使用Bookings_Server.OData.Models;
使用Microsoft.AspNet.OData;
命名空间预订\u Server.OData.Controllers
{
[产生(“应用程序/json”)]
公共类CustomerController:ODataController
{
私有只读数据上下文_上下文;
公共CustomerController(DataContext上下文)
{
_上下文=上下文;
}
//获取:odata/客户
[启用查询(页面大小=20)]
public IQueryable Get()=>\u context.Customers.AsQueryable();
/*
public IActionResult Get()
{
返回Ok(_context.Customers.AsQueryable());
}
*/
}
}
文件路径:startup.cs
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNet.OData.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Bookings_Server
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, BookingsModelBuilder BookingsModelBuilder)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors("cors");
// app.UseMvc();
// Added for Odata config
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("ODataRoutes", "odata", BookingsModelBuilder.GetEdmModel(app.ApplicationServices));
});
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("cors", builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}
));
var connection = @"Server=(localdb)\mssqllocaldb;Database=BookingsDB;Trusted_Connection=True;";
services.AddDbContext<EF.DataContext>(options => options.UseSqlServer(connection));
// Add OData configuration
services.AddOData();
services.AddTransient<BookingsModelBuilder>();
services.AddMvc().AddJsonOptions(opt =>
{
opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
}
}
}
使用系统;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.AspNet.OData.Extensions;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Serialization;
命名空间预订服务器
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、BookingModelBuilder BookingModelBuilder)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
附录UseCors(“cors”);
//app.UseMvc();
//为Odata配置添加了
app.UseMvc(routeBuilder=>
{
routeBuilder.MapODataServiceRoute(“ODataRoutes”,“odata”,BookingsModelBuilder.GetedModel(app.ApplicationServices));
});
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddCors(options=>options.AddPolicy(“cors”,builder=>
{
建设者
.AllowAnyOrigin()
.AllowAnyMethod()
阿洛瓦尼海先生
[HttpGet]
[EnableQuery]
public async Task<Skill[]> GetFilteredODataList(ODataQueryOptions<Skill> q)
{
var skillsQuery = this._context.Skills.AsQueryable();
if (q?.Filter != null)
{
skillsQuery = q.Filter.ApplyTo(skillsQuery, new ODataQuerySettings()) as IQueryable<Skill>;
}
return await skillsQuery.ToArrayAsync();
}
[ODataRoute("customers")]
using Microsoft.AspNet.OData.Routing;
// GET: odata/customers
[ODataRoute("customers")]
[EnableQuery(PageSize = 20)]
public IQueryable<Customer> Get() => _context.Customers.AsQueryable();