带有版本和路由属性的web api 2路由

时间:2015-05-08 09:28:12

标签: c# asp.net-mvc-5 asp.net-web-api2 asp.net-mvc-routing

在我的项目中,我有两个版本的API。从这个Post我明白自定义控件选择器需要编写才能获得不同的版本(因为webapi会查找控制器名称匹配并忽略它们所在的命名空间)。

一切正常,我可以拨打不同的版本。

但是,我也使用来自web api 2的新数据属性路由,并且在使用时,客户控制选择器无法正确处理路由。

我使用的路由数据属性如下。

(版本之间的V01不同,因此它可以是V02)

[Route("api/v01/enumeration/{type}/{subtype}")]

这也可以在末尾包含一些额外的查询字符串参数

[Route("api/V01/user/readbyreference")] 

这会使用?id=EMAIL|email.domain.com

的查询字符串

可以找到客户控制选择器的代码here

我可以看到问题是GetRouteVariable来获取所需的命名空间部分和控制器,但我想知道是否有其他人必须做这样的事情,如果他们有任何解决方法。

我会调查,如果我找到了一些我会在这里更新的内容,但如果你有任何问题,请告诉我。

谢谢

标记

1 个答案:

答案 0 :(得分:2)

After a bit of digging I have found out that attribute routing goes via a different path.

So to handle attribute routing in the <Import Project="..\..\packages\Xamarin.Forms.1.2.3.6257\build\portable-win+net45+wp80+MonoAndroid10+MonoTouch10\Xamarin.Forms.targets" you need to grab the Condition="Exists('..\..\packages\Xamarin.Forms.1.2.3.6257\build\portable-win+net45+wp80+MonoAndroid10+MonoTouch10\Xamarin.Forms.targets')" /> values and then perform the needed action on the result to get the namespace and controller. The below needs tidying up but it at least gives you the idea of what is done to process data attribute routing in your custom control selector

GetRouteVariable

Cheers Mark