ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-22 11:53:57  阅读:374  来源: 互联网

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


我试图在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控制器.因此,您需要使用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.运行申请

结果:

enter image description here

标签:c,asp-net,odata,asp-net-core,asp-net-core-webapi
来源: https://codeday.me/bug/20190622/1263121.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有