I need help with the task. I must develop an ASP.NET Core 8 Web API with OData because it will be connected to Excel.
After searching, I found the Attach project which was develop in .NET Core 2.1. This project found success but I have problems to adapt it.
Project link : https://github.com/OData/ODataSamples/tree/master/WebApiCore/DynamicEdmModelCreation/DynamicEdmModelCreation
Could someone please assist me with this task?.
In the program file I adder the next to code:
builder.Services.AddControllers()
.AddOData(options =>
{
options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null);
options.CustomMapODataServiceRoute("odata", "odata/{dataSource}", serviceProvider);
});
And I adapted the class
public static class ODataRouteBuilderExtensions
{
public static void CustomMapODataServiceRoute(this ODataOptions odataOptions, string routeName, string routePrefix, IServiceProvider sp)
{
odataOptions
.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null)
.AddRouteComponents(routePrefix, GetEdmModel(sp)
, services =>
{
// Agregar convenciones de enrutamiento OData
IList<IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
routingConventions.Insert(0, new MatchAllRoutingConvention());
services.AddSingleton<IEnumerable<IODataRoutingConvention>>(routingConventions);
});
//// route.Constraints.
// IRouter customRouter = routeBuilder.ServiceProvider.GetService<IRouter>();
//// Get constraint resolver.
// IInlineConstraintResolver inlineConstraintResolver = sp.GetRequiredService<IInlineConstraintResolver>();
}
internal static IEdmModel GetEdmModel(IServiceProvider sp)
{
var httpContextAccessor = sp.GetRequiredService<IHttpContextAccessor>();
var httpContext = httpContextAccessor.HttpContext;
if (httpContext == null)
return DataSourceProvider.GetEdmModel(Constants.MyDataSource.ToString());
// Acceder al parámetro "dataSource" de la URL
string sourceString = httpContext.Request.RouteValues["dataSource"]?.ToString() ?? Constants.MyDataSource.ToString();
// Obtener el modelo EDM basado en la fuente de datos
IEdmModel model = DataSourceProvider.GetEdmModel(sourceString);
return model;
}
}
But when I navigate to the URL
http://localhost:1589/odata/mydatasource/Products
the controller method is not called.