我試圖實現Attribute Routing和VersionedRoute
從RoutingConstaints Sample,但是當我在控制器上同時使用時,versioned屬性不再起作用。Web API 2不識別多個屬性的路由(版本控制)
我需要在屬性上修改哪些內容才能讓它與屬性路由很好地發揮作用?
對於代碼示例下載示例項目(或只是看一些文件,從上面的鏈接),然後修改路由這樣:
// When I use the RoutePrefix, VersionedRoute no longer works (Sending "Api-Version" through http header doesn't route correctly
// If I remove the RoutePrefix I can use VersionedRoute again
// What do I need to change in its code to be able to use both?
[VersionedRoute("api/Customers", 1)] // This route would be used as http://url/api/customers with a header of "api-version: 1"
[RoutePrefix("api/v1/Customers")] // This route would be used purely through url versioning of http://url/api/v1/Customers
public class CustomersV1Controller : ApiController {
/* Other stuff removed */
[VersionedRoute("api/Customer", 1)] // I'd rather not have to use this here at all and just use a single one on the class, but having both nor just one on either works right now.
[Route("")]
public IHttpActionResult Get()
{
return Json(_customers);
}
}
編輯:請讓我知道,如果你需要更多的信息,甚至張貼想法或東西來嘗試:)
編輯2:這是一個我想從Troy Hunt的博客中嘗試做的一個例子:http://www.troyhunt.com/2014/02/your-api-versioning-is-wrong-which-is.html
Edit3:這是我想編寫的儘可能接近的代碼,因爲它會減少很多開銷和魔法字符串。
[VersionedRoute("api/Customers", 1)] // This route would be used as http://url/api/customers with a header of "api-version: 1"
[RoutePrefix("api/v1/Customers")] // This route would be used purely through url versioning of http://url/api/v1/Customers
public class CustomersV1Controller : ApiController {
/* Other stuff removed */
[Route("")]
public IHttpActionResult Get()
{
// Removed
return Ok(customers);
}
[Route("{id:int}")]
public IHttpActionResult GetById(int id)
{
// Removed
return Ok(customer);
}
}
[VersionedRoute("api/Customers", 2)] // This route would be used as http://url/api/customers with a header of "api-version: 2"
[RoutePrefix("api/v2/Customers")] // This route would be used purely through url versioning of http://url/api/v2/Customers
public class CustomersV2Controller : ApiController {
/* Other stuff removed */
[Route("")]
public IHttpActionResult Get()
{
// Removed
return Ok(customersThatAreDifferentThanV1);
}
[Route("{id:int}")]
public IHttpActionResult GetById(int id)
{
// Removed
return Ok(customerThatIsDifferent);
}
}
編輯:最後一次碰撞,試圖只需要在控制器屬性級而不是每個行爲每個路由寫一次路由版本信息。
什麼是你想要做什麼呢? – DavidG 2014-10-17 08:54:18
@DavidG我正在嘗試使用'VersionedRoute(「Customer」,1)'這將允許我使用http頭來提供版本#,以及使用'RoutePrefix(「v1/Customer /」)通過url進行版本控制。基本上我想支持標題和網址版本。但是,當我應用'RoutePrefix'時'VersionedRoute'屬性停止工作。 – John 2014-10-17 16:21:33
設置項目時,你的配置是什麼樣的? – entropic 2014-10-17 16:44:27