gpt4 book ai didi

c# - 在 ASP.NET Core Web API 中创建返回 OData 的 enbdpoints

转载 作者:太空狗 更新时间:2023-10-30 01:13:44 24 4
gpt4 key购买 nike

我正在尝试在 ASP.NET Core Web API 中创建 OData 端点。

我使用模板创建了一个新的 ASP.NET Core Web API,并向其中添加了 Microsoft.AspNetCore.OData 包 (v7.0.0-beta1),假设它是必需的。

我找不到任何关于如何开始使用它的文档。如果有人能告诉我如何让默认的 ValuesController 简单地返回 OData 而不是 Json,那就太好了。

最佳答案

I created a new ASP.NET Core Web API using the template and added the Microsoft.AspNetCore.OData package (v7.0.0-beta1) to it assuming it is required.

I can't find any documentation on how to get started with this. If anyone can tell me how I would simply turn the default ValuesController to return OData instead of Json that would be great.

根据您的描述,我建议您可以尝试按照以下步骤创建 net core odata web api。

1.安装Microsoft.AspNetCore.OData 7.0.0-beta1

2.安装Microsoft.EntityFrameworkCore

3.创建模型类和DBContext类。

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; }

}

4.创建Controller,在OData早期版本中可以继承ODataController。但是在 ASP.NET Core 中,没有可用的 OData Controller 。因此,您需要创建一个具有 OData 属性的普通 Controller 。

public class PersonController : Controller
{
private readonly ApplicationDbContext _appDbContext;
public PersonController(ApplicationDbContext sampleODataDbContext)
{
_appDbContext = sampleODataDbContext;
}

[EnableQuery]
public IActionResult Get()
{
return Ok(_appDbContext.Persons.AsQueryable());
}
}

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;
}
}

6.打开包管理器控制台创建表:Add-Migration InitialCreate update-database

7.运行应用

结果:

enter image description here

关于c# - 在 ASP.NET Core Web API 中创建返回 OData 的 enbdpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48551571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com