0

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.

1 Answer 1

0

It seems you want to use Microsoft.AspNetCore.OData 8.x version. Why don't you try 'https://github.com/OData/AspNetCoreOData/tree/main/sample/ODataDynamicModel'?

This is the correpsonding sample using Microsoft.AspNetCore.OData 8.x version.

Feel free to file issue if you have any problems to use the sample.

By the way, you can also find more similar samples (but different ways) at:

https://github.com/xuzhg/WebApiSample/tree/main/v8.x/DynamicRouteSample https://github.com/xuzhg/WebApiSample/tree/main/v8.x/ODataDyncRouteSample

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answer!.. it was more usefull

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.