Customizing URI templates using Odata

Asked

Viewed 94 times

4

I am working on a project that contains the package Microsoft.AspNet.WebApi.OData, which offers a well-simplified implementation to support Odata V3 by simply using the Attribute EnableQueryAttribute 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 as it handles URI resolution and covers route conventions, as can be 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 format of the URI that the package expects does not follow the REST standard and I would like to customize it as well, as is possible in the default Webapi routes.

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?

1 answer

2


In Asp.Net Core you have to set up like this:

            app.UseMvc(b =>
            {
                var odataOptions = new Microsoft.AspNet.OData.ODataOptions() { UrlKeyDelimiter = ODataUrlKeyDelimiter.Slash };
                b.SetDefaultODataOptions(odataOptions);
                b.MapODataServiceRoute("odata", "odata", GetEdmModel());
            });
  • Although I’m not using Asp.net-core yet, I believe this answers the question. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.