代码之家  ›  专栏  ›  技术社区  ›  Blake Rivell

正在创建在ASP中返回OData的enbdpoints。NET核心Web API

  •  4
  • Blake Rivell  · 技术社区  · 7 年前

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

    我创建了一个新的ASP。NET核心Web API使用模板并添加了Microsoft。AspNetCore。假设需要OData包(v7.0.0-beta1)。

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

    1 回复  |  直到 7 年前
        1
  •  4
  •   Community CDub    4 年前

    我创建了一个新的ASP。NET核心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类。

    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、创建一个控制器,在早期版本的OData中,您可以从ODataController继承。但在ASP中。NET核心,没有可用的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、打开package manager控制台创建表:Add Migration InitialCreate update database

    7、运行应用程序

    结果:

    enter image description here