C# 在ASP.NET核心Web API中创建返回OData的enbdpoint
我正在尝试在ASP.NET核心Web API中创建OData端点 我使用该模板创建了一个新的ASP.NET Core Web API,并将Microsoft.AspNetCore.OData包(v7.0.0-beta1)添加到其中(假设需要) 我找不到任何关于如何开始这项工作的文档。如果有人能告诉我如何简单地将默认值控制器转换为返回OData而不是Json,那就太好了 我使用该模板创建了一个新的ASP.NET Core Web API,并将Microsoft.AspNetCore.OData包(v7.0.0-beta1)添加到其中(假设需要) 我找不到任何关于如何开始这项工作的文档。如果有人能告诉我如何简单地将默认值控制器转换为返回OData而不是Json,那就太好了 根据您的描述,我建议您可以尝试按照以下步骤创建net core odata web api 1.安装Microsoft.AspNetCore.OData 7.0.0-beta1 2.安装Microsoft.EntityFrameworkCore 3.创建模型类和DBContext类C# 在ASP.NET核心Web API中创建返回OData的enbdpoint,c#,asp.net,asp.net-core,odata,asp.net-core-webapi,C#,Asp.net,Asp.net Core,Odata,Asp.net Core Webapi,我正在尝试在ASP.NET核心Web API中创建OData端点 我使用该模板创建了一个新的ASP.NET Core Web API,并将Microsoft.AspNetCore.OData包(v7.0.0-beta1)添加到其中(假设需要) 我找不到任何关于如何开始这项工作的文档。如果有人能告诉我如何简单地将默认值控制器转换为返回OData而不是Json,那就太好了 我使用该模板创建了一个新的ASP.NET Core Web API,并将Microsoft.AspNetCore.OData包(
public class Person
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Age { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
public DbSet<Person> Persons { get; set; }
}
5.修改启动类代码,添加OData中间件和OData路由
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddOData();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//Adding Model class to OData
var builder = GetEdmModel(app.ApplicationServices);
builder.EntitySet<Person>(nameof(Person));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc((routebuilder =>
{
routebuilder.MapODataServiceRoute("odata","odata", builder.GetEdmModel());
}));
}
private static ODataConventionModelBuilder GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
return builder;
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext谢谢!这一切工作需要实体框架吗?假设我想使用Dapper或其他ORM来访问我的数据。与@BlakeRivell有相同的问题,我正在尝试将OData与Dapper一起使用。有可能吗?我可以编写自己的数据访问逻辑吗?
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddOData();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//Adding Model class to OData
var builder = GetEdmModel(app.ApplicationServices);
builder.EntitySet<Person>(nameof(Person));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc((routebuilder =>
{
routebuilder.MapODataServiceRoute("odata","odata", builder.GetEdmModel());
}));
}
private static ODataConventionModelBuilder GetEdmModel(IServiceProvider serviceProvider)
{
var builder = new ODataConventionModelBuilder(serviceProvider);
return builder;
}
}