我正在尝试使用 OData 和 EntityFramework 创建一个具有“简单”Web api 的新 ASP.NET Core 项目。我以前曾将 OData 与旧版本的 ASP.NET 一起使用。
我已经设置了一个仅具有简单获取功能的控制器。我已经设法让它使用基本的 OData 命令作为过滤器和顶部,但我无法让扩展命令工作。我认为这是因为我不太清楚如何在 Startup.cs 中设置它。我尝试了很多事情,包括遵循 Github 上的一些 odata 示例:
https://github.com/OData/WebApi/tree/vNext/vNext/samples/ODataSample.Web https://github.com/OData/WebApi/tree/vNext/vNext/samples/ODataSample.Web
https://github.com/bigfont/WebApi/tree/master/vNext/samples/ODataSample.Web https://github.com/bigfont/WebApi/tree/master/vNext/samples/ODataSample.Web
在我的启动文件中,我尝试从 Service 类中排除一些属性,但这些属性根本没有效果。所以问题可能出在我使用 IDataService 接口的方式上。 (ApplicationContext 像示例中一样实现它)
需要明确的是,我正在使用完整的 .NET Framework 而不仅仅是 .Core 框架创建 ASP.NET Core Web api。我当前的代码是两个示例中最好/最差的组合,并且在我可以过滤 WebAPI 但无法使其扩展或隐藏属性的意义上工作。
任何人都可以看到我缺少什么 og 有一个可用的 ASP.NET Odata 示例。我对startup.cs 中的整个设置不熟悉?我想我正在寻找能够完成这项工作的人。
控制器
[EnableQuery]
[Route("odata/Services")]
public class ServicesController : Controller
{
private IGenericRepository<Service> _serviceRepo;
private IUnitOfWork _unitOfWork;
public ServicesController(IGenericRepository<Service> serviceRepo, IUnitOfWork unitOfWork)
{
_serviceRepo = serviceRepo;
_unitOfWork = unitOfWork;
}
[HttpGet]
public IQueryable<Service> Get()
{
var services = _serviceRepo.AsQueryable();
return services;
}
}
startup
using Core.DomainModel;
using Core.DomainServices;
using Infrastructure.DataAccess;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.OData.Extensions;
namespace Web
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc().AddWebApiConventions();
services.AddSingleton<ApplicationContext>(_ => ApplicationContext.Create());
services.AddSingleton<IDataService, ApplicationContext>();
services.AddOData<IDataService>(builder =>
{
//builder.EnableLowerCamelCase();
var service = builder.EntitySet<Service>("Services");
service.EntityType.RemoveProperty(x => x.CategoryId);
service.EntityType.RemoveProperty(x => x.PreRequisiteses);
});
services.AddSingleton<IGenericRepository<Service>, GenericRepository<Service>>();
services.AddSingleton<IUnitOfWork, UnitOfWork>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
app.UseApplicationInsightsRequestTelemetry();
//var builder = new ODataConventionModelBuilder(app.ApplicationServices.GetRequiredService<AssembliesResolver>());
//var serviceCtrl = nameof(ServicesController).Replace("Controller", string.Empty);
//var service = builder.EntitySet<Service>(serviceCtrl);
//service.EntityType.RemoveProperty(x => x.CategoryId);
app.UseOData("odata");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
Project.json 依赖项
"dependencies": {
"Microsoft.ApplicationInsights.AspNetCore": "1.0.2",
"Microsoft.AspNet.Identity.EntityFramework": "2.2.1",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Identity": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Microsoft.AspNetCore.OData": "1.0.0-rtm-00015",
"dnx-clr-win-x86": "1.0.0-rc1-update2",
"Microsoft.OData.Core": "7.0.0",
"Microsoft.OData.Edm": "7.0.0",
"Microsoft.Spatial": "7.0.0"
}