I am trying to create OData endpoints in ASP.NET Core Web API.
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.
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.
According to your description, I suggest you could try to follow below steps to create net core odata web api.
1.Install the Microsoft.AspNetCore.OData 7.0.0-beta1
2.Install the Microsoft.EntityFrameworkCore
3.Create a model class and DBContext class.
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.Create a Controller, in earlier versions of OData you can inherit from ODataController. But in ASP.NET Core, there is no OData controller available. So you need to create a normal controller, with OData attributes.
public class PersonController : Controller
{
private readonly ApplicationDbContext _appDbContext;
public PersonController(ApplicationDbContext sampleODataDbContext)
{
_appDbContext = sampleODataDbContext;
}
[EnableQuery]
public IActionResult Get()
{
return Ok(_appDbContext.Persons.AsQueryable());
}
}
5.Modify the startup class code to add OData middleware and OData routing.
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.Open package manager console to create the table: Add-Migration InitialCreate update-database
7.Run the application
Result:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.