如何声明参数作为OData的preFIX [英] How to declare a parameter as prefix on OData
问题描述
使用的WebAPI正常ApiController,我做了如下code定义每个ApiControllers动态地图路径模板:
Using a normal ApiController in WebApi, I made the following code to define a dynamic map route template for every ApiControllers:
config.Routes.MapHttpRoute(
name: "Sample",
routeTemplate: "{sessionId}/{controller}"
);
我要实现类似行为,但每ODataController。
我试过以下code,但它不工作:
I want to achieve a similar behaviour but for every ODataController. I've tried the following code, but it doesn't work:
config.MapODataServiceRoute(
routeName: "HSODataRoute",
routePrefix: "{sessionId}/",
model: GetEdmModel());
任何想法,这是如何的OData做?我有点新的它和互联网是缺乏这方面的信息。
Any idea how this is made in OData? I'm kinda new to it and the internet is lack of information about this.
要更具体地:{的sessionId}不应该是一个常数,而是用一个GUID值的参数
To be more specific: {sessionId} isn't supposed to be a constant, but a parameter with a Guid value.
推荐答案
一些测试后,我发现,声明MapODataServiceRoute是不够的!
您需要还添加MapHttpRoute此外,由于ODataController从ApiController派生
After a few tests I've found out that declaring MapODataServiceRoute is not enough! You need also to add MapHttpRoute also, since ODataController derives from ApiController
config.Routes.MapHttpRoute(
name: "Sample",
routeTemplate: "{sessionId}/{controller}"
);
config.MapODataServiceRoute(
routeName: "HSODataRoute",
routePrefix: "{sessionId}/",
model: GetEdmModel());
我发现了这一点,因为我删除MapHttpRoute后,我已经开始得到404没有发现,当我加入MapHttpRoute资源可能被发现。
I discovered this because after I removed MapHttpRoute, I've started to get 404 not found, and when I added MapHttpRoute the resource could be found.
更新:
这是我想出解决这个问题的最终解决方案是张贴在这里:传递参数在OData的的WebAPI网址。
The final solution that I've come up to solve this issue was posted here: Pass Parameters in OData WebApi Url.
这篇关于如何声明参数作为OData的preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!