I need help with the task. I must develop Web Api Net 8 with Odata because The Api will connectet to Excel.
After searchin, I can 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 call the “http://localhost:1589/odata/mydatasource/Products” the cotroller is not called.
MoranJE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.