带组合键的Odata v3 Web Api导航 [英] Odata v3 Web Api navigation with composite key
问题描述
我有一个使用Odata v3的Web Api,其中有些实体是复合密钥,例如这样的
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; }
}
我关注了这篇msdn文章,并创建了NavigationRoutingConvention 一个>.该应用程序现在可以很好地处理复合键.但是,这样的导航链接不起作用:
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
我不断收到找不到与请求匹配的HTTP资源",好像该方法未在控制器中实现.顺便说一下,这是控制器方法:
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;
}
我找到了这个问题讨论完全相同的问题,但我还没有弄清楚尼康(Nikon)的处理方式问题.
I found this question talking about exactly the same problem, but I haven't figured out how Nikon handled the issue.
推荐答案
爱德华多
来自MSDN文章在ASP.NET Web中支持复合键API OData
public class CompositeKeyRoutingConvention : EntityRoutingConvention
{
....
}
上述路由约定可以涵盖以下Uri模板:
The above routing convention can cover the following Uri templates:
- 〜/entityset/key
- 〜/entityset/key/cast
但是,它不能覆盖〜/entityset/key/navigation
修复很简单,只是从 NavigationRouteConvention
派生的,如下所示
The fix is simple, just derived from NavigationRouteConvention
as below
public class CompositeKeyRoutingConvention : NavigationRoutingConvention
{
...
}
下面是调试信息:
请确保:如果要同时支持这两个Uris:
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
您必须具有两个自定义路由约定,一个自 EntityRoutingConvention
派生,另一个自 NavigationRoutingConvention
派生.
You must have two custom routing conventions, one derived from EntityRoutingConvention
, the other one derived from NavigationRoutingConvention
.
希望它会有所帮助.谢谢.
Hope it can help. Thanks.
这篇关于带组合键的Odata v3 Web Api导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!