Customize URI templates using OData

4

I'm working on a project that contains the Microsoft.AspNet.WebApi.OData package, which offers a simplified implementation for OData V3 support, just use the EnableQueryAttribute attribute in the methods or classes where we want to enable support:

[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Filter |
                                   AllowedQueryOptions.OrderBy |
                                   AllowedQueryOptions.Select)]
public IQueryable<Vaga> GetVagas()
{
    return db.Vagas;
}

However, the latest version of the package - which supports OData V4 - does not offer such a simplified implementation because it handles URI resolution and covers route conventions, as seen in one of the examples of official documentation .

In the documentation I found ways to customize the resolutions of controllers and actions, but the URI format that the expected package does not follow the REST pattern and I would like to customize it too, as is possible in the standard WebApi routes. p>

Standard WebApi

config.Routes.MapHttpRoute(
    name: $"MyRoute",
    routeTemplate: $"api/{{controller}}/{{action}}/{{id}}",
    defaults: new { id = RouteParameter.Optional }
    );

With OData

var builder = new ODataConventionModelBuilder();
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null);

builder.EntitySet<Produto>("Produtos").EntityType.HasMany(p => p.Fornecedores);
builder.EntitySet<Fornecedor>("Fornecedores").EntityType.HasKey(l => l.IdFornecedor);

config.MapODataServiceRoute($"MyODataRoutes", $"api/odata/", builder.GetEdmModel());
 _________________________________________________________________________________________________________
|                       |      *Formato suportado pelo pacote*      |         *Formato esperado*          |
|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
|                       | site.com/Produtos(2)/Fornecedor(5)        | site.com/Produtos/2/Fornecedor/5    |
|*Template equivalente* | ~/entityset/key/navigation/key            | ~/entityset/key/navigation/key      |
 ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Does anyone know how it could be done?

    
asked by anonymous 14.08.2018 / 16:23

1 answer

2

In Asp.Net Core you have to configure it like this: app.UseMvc(b => { var odataOptions = new Microsoft.AspNet.OData.ODataOptions() { UrlKeyDelimiter = ODataUrlKeyDelimiter.Slash }; b.SetDefaultODataOptions(odataOptions); b.MapODataServiceRoute("odata", "odata", GetEdmModel()); });

    
13.09.2018 / 21:49