如何在ASP.NET Core 2.1中使用OData? [英] How to use OData in ASP.NET Core 2.1?

查看:133
本文介绍了如何在ASP.NET Core 2.1中使用OData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在ASP中使用OData.下面是我的代码:

I try to use OData in an ASP. Bellow my code:

//============== Startup.cs
public void ConfigureServices(IServiceCollection services) {
    services.AddDbContext<EntriesContext>(
        opt => opt.UseMySql("server=localhost;database=mydb;user=myusr;password=mypass",
        mysqlOptions =>{mysqlOptions.ServerVersion(new Version(5..), ServerType.MySql);}));

    services.AddOData();
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        ...
        app.UseMvc(b => { b.MapODataServiceRoute("odata", "odata", GetEdmModel()); });
    }

    private static IEdmModel GetEdmModel() {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Entry>("Entries");
        return builder.GetEdmModel();
    }

控制器:

[Route("api/[controller]")]
[ApiController]
[EnableCors("AllowMyOrigin")]
public class EntriesController : ODataController
{
    private readonly EntriesContext _context;

    public EntriesController(EntriesContext context) {
        _context = context;
    }

    [HttpGet]
    [EnableQuery]
    public ActionResult<List<Entry>> GetAll() {
        return _context.Entries.ToList();
    }

上下文:

public class EntriesContext : DbContext
{
    public EntriesContext(DbContextOptions<EntriesContext> options) : base(options) { }
    public DbSet<Entry> Entries { get; set; }
}

但是,对于我来说不清楚,我应该使用什么路径来获取条目(没有OData,我会使用localhost:9000/api/entries,但是现在我很困惑).

however, is not clear for me, what path should I use to get the entries (without OData I would use localhost:9000/api/entries, but now I am confused).

我尝试做https://localhost:44384/odata/entrieshttps://localhost:44384/odata/api/entries,但得到404

I tried to do https://localhost:44384/odata/entries and https://localhost:44384/odata/api/entries but I get 404

我试图像这样注释控制器的路线

I tried to comment the controller's route, like this

//[Route("api/[controller]")]
//[ApiController]
[EnableCors("AllowMyOrigin")]
public class EntriesController : ODataController 

并修改了操作

[HttpGet]
[EnableQuery]
public IActionResult Get() {
    return Ok(_db.Entries);
}

然后我尝试https://localhost:44384/odata/Entries并获得了条目的完整列表...但是,https://localhost:44384/odata/Entries?$take=2不起作用:400错误的请求:Parameter name: $take' is not supported."

I tried then https://localhost:44384/odata/Entries and gotthe full list of the entries... However, the https://localhost:44384/odata/Entries?$take=2 does not work: 400 Bad Request: Parameter name: $take' is not supported."

推荐答案

ODataController继承时,无需使用[ApiController]批注.删除注释,它应能按预期工作. 为了能够使用$take进行查询,您需要对其进行设置:

You don't need to use [ApiController] annotation when you inherit from ODataController. Remove the annotation and it should work as expected. To be able to query using $take you need to setup it:

app.UseMvc(b =>
      {
        b.Take();
        b.MapODataServiceRoute("odata", "odata", GetEdmModel());
      });

您还可以添加其他操作,例如Select().Expand().Filter().OrderBy().MaxTop(100).Count()

You can also add other actions, e.g. Select().Expand().Filter().OrderBy().MaxTop(100).Count()

这篇关于如何在ASP.NET Core 2.1中使用OData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆