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?
Although I’m not using Asp.net-core yet, I believe this answers the question. Thank you
– Diego Rafael Souza