1) 啓用。在App_Start - WebApiConfig.cs下api
//在Register函數添加以下代碼: config.MapHttpAttributeRoutes();
2) 使用。Controller中使用Route關鍵字函數
[Route("customers/{customerId}/orders")] public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }
3) 與基於約定的路由結合使用。在App_Start - WebApiConfig.cs下spa
// Attribute routing. config.MapHttpAttributeRoutes(); // Convention-based routing. config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );