Odata v3 Web Api Navigation with Composite Key

I have a Web Api using Odata v3, and some entities are composite, like this one:

public class AerodromoAdministracaoData { [Key] [Column("idAerodromo", Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public short IdAerodromo { get; set; } [Key] [Column("data", Order = 1, TypeName = "date")] public DateTime Data { get; set; } public virtual Aerodromo Aerodromo { get; set; } } 

I followed this msdn article and created the NavigationRoutingConvention site . Now the application processes compound keys. However, navigation links like this do not work:

 http://localhost/WebApiV3/AerodromoAdministracaoData%28idAerodromo=1,data=%272014-10-24%27%29/Aerodromo 

I keep getting "An HTTP resource not found that matches the request", as if this method were not implemented in the controller. By the way, this is the controller method:

  [EnableQuery] public Aerodromo GetAerodromo([FromODataUri] short idAerodromo, [FromODataUri] DateTime data) { AerodromoAdministracaoData result = Store.AerodromoAdministracaoData.Find(idAerodromo, data); if (result == null) { throw new HttpResponseException(new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.NotFound)); } return result.Aerodromo; } 

I found this question talking about exactly the same problem, but I did not understand how Nikon fixed the problem.

+6
source share
1 answer

Eduardo

From an MSDN Article Composite Key Support in OData ASP.NET Web API

 public class CompositeKeyRoutingConvention : EntityRoutingConvention { .... } 

The above routing convention may cover the following Uri patterns:

  • ~ / EntitySet / key
  • ~ / EntitySet / key / cast

But it cannot cover ~ / entityset / key / navigation

The fix is ​​simple , just obtained from NavigationRouteConvention , as shown below

 public class CompositeKeyRoutingConvention : NavigationRoutingConvention { ... } 

The following is debugging information: The debug information:

Please make sure: if you want to support as Uris:

  • / AerodromoAdministracaoData% 28idAerodromo = 1, data =% 272014-10-24% 27% 29
  • / AerodromoAdministracaoData% 28idAerodromo = 1, data =% 272014-10-24% 27% 29 / Aerodromo

You must have two user routing conventions, one from EntityRoutingConvention and one from NavigationRoutingConvention .

Hope this helps. Thanks.

+2
source

Source: https://habr.com/ru/post/978038/


All Articles