我将 OData 添加到我的项目中并测试了一些场景。我一直坚持向 OData 添加版本控制,但我最终添加了它,没有出现任何重大问题。现在,我可以毫无问题地使用该版本调用端点,但它没有出现在 Swagger 中。
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntityType<Order>();
modelBuilder.EntitySet<Customer>("Customers");
builder.Services.AddControllers().AddOData(
options => options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null).AddRouteComponents(
"api/v{version:apiVersion}/",
modelBuilder.GetEdmModel()));
builder.Services.AddApiVersioning(opt =>
{
opt.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.ReportApiVersions = true;
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
new HeaderApiVersionReader("x-api-version"),
new MediaTypeApiVersionReader("x-api-version"));
});
builder.Services.AddVersionedApiExplorer(setup =>
{
setup.GroupNameFormat = "'v'VVV";
setup.SubstituteApiVersionInUrl = true;
});
控制器
namespace Aimtec.DataIntegrator.WebApi.Controllers.v1
{
[ApiVersion("1.0")]
public class CustomersController : ODataController
{
private static Random random = new Random();
private static List<Customer> customers = new List<Customer>(
Enumerable.Range(1, 3).Select(idx => new Customer
{
Id = idx,
Name = $"Customer {idx}",
Orders = new List<Order>(
Enumerable.Range(1, 2).Select(dx => new Order
{
Id = (idx - 1) * 2 + dx,
Amount = random.Next(1, 9) * 10
}))
}));
[EnableQuery]
public ActionResult Get()
{
return Ok(customers);
}
}
}
此外,您是否碰巧有关于如何为 OData 一般添加版本控制的好指南?