我有一个并排的Web API 2.2 APIController和OData v4 ODataController。我的APIController在内部使用路由属性(没有预定义的路由默认值):
[RoutePrefix("api")]
public class MyController : ApiController
{
[HttpGet]
[Route("My")]
public IHttpActionResult Get()
{
//Code Here
}
[HttpGet]
[Route("My")]
public IHttpActionResult Get([FromUri] String mykey)
{
//Code Here
}
}
因此通过./api/My和./api/My/?mykey=value
路由到我尝试设置我的ODataController以跟随类似的诉讼:
[ODataRoutePrefix("My")]
public class oMyController : ODataController {
[HttpGet]
public IHttpActionResult Get(ODataQueryOptions<FileModel> queryOptions) {
//Code Here
}
[HttpGet]
[ODataRoute("({mykey})")]
public IHttpActionResult Get([FromODataUri] String mykey) {
//Code Here
}
}
提前定义odata路线,如下所示:
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<MyModel>("My");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: "odata",
model: builder.GetEdmModel()
);
但尝试访问./odata/My和./odata/My(value)最终会进入我的APIController而不是ODataController。
如何使用不同的前缀,但使用相同的名称来路由这些前缀,并让它们转到相应的控制器。如果我可以阻止它,我不希望每条路线都有不同的名称,前缀应该处理所有事情,但由于某种原因它们不是。
答案 0 :(得分:13)
[ODataRoutePrefix("My")]
public class oMyController : ODataController {
[HttpGet]
[ODataRoute()] // <---<< This was the key to proper OData routing
public IHttpActionResult Get(ODataQueryOptions<FileModel> queryOptions) {
//Code Here
}
[HttpGet]
[ODataRoute("({mykey})")]
public IHttpActionResult Get([FromODataUri] String mykey) {
//Code Here
}
}