【问题标题】:Creating enbdpoints that return OData in ASP.NET Core Web API在 ASP.NET Core Web API 中创建返回 OData 的端点
【发布时间】:2018-07-11 02:50:20
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# asp.net asp.net-core odata asp.net-core-webapi


    【解决方案1】:

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

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

    根据您的描述,我建议您可以尝试按照以下步骤创建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 控制器。所以你需要创建一个普通的控制器,带有 OData 属性。

    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.运行应用程序

    结果:

    【讨论】:

    • 谢谢!这一切都需要实体框架吗?假设我想使用 Dapper 或其他 ORM 来访问我的数据。
    • 遇到与@BlakeRivell 相同的问题,我正在尝试将 OData 与 dapper 一起使用。可能吗?我可以编写自己的数据访问逻辑吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2020-04-18
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2015-08-17
    • 2017-01-17
    相关资源
    最近更新 更多