Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odata v3 Web Api navigation with composite key

I have a Web Api using Odata v3, with some entities a composite key, 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 a NavigationRoutingConvention. The application handles composite keys fine now. However, navigation Links like this one don't work:

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

I keep getting a "No HTTP resource was found that matches the request" as if the method was 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 haven't figured out how Nikon handled the issue.

like image 858
Eduardo Avatar asked Nov 11 '14 18:11

Eduardo


1 Answers

Eduardo

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

public class CompositeKeyRoutingConvention : EntityRoutingConvention
{
   ....
}

The above routing convention can cover the following Uri templates:

  • ~/entityset/key
  • ~/entityset/key/cast

But, it can't cover ~/entityset/key/navigation

The fix is simple, just derived from NavigationRouteConvention as below

public class CompositeKeyRoutingConvention : NavigationRoutingConvention
{
    ...
}

Below is the debug information: The debug information:

Please make sure: if you want support both Uris:

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

You must have two custom routing conventions, one derived from EntityRoutingConvention, the other one derived from NavigationRoutingConvention.

Hope it can help. Thanks.

like image 167
Sam Xu Avatar answered Oct 31 '22 03:10

Sam Xu