Q:

Attribute Routing doesn't work

I'm creating a rest api in visual studio express 2013.
I have 2 controllers: one for orders and one for clients.
I've already created the following:
/api/clients GET information about all clients
/api/clients/1 GET information about client with id = 1
/api/orders/10 GET information about order with id = 10
Now, I want to create this:
/api/clients/1/orders - GET information about all orders of client with id 1
I've read about attribute routing, but I can't make it work.
OrdersController.cs
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class OrdersController : ApiController
{
    public Order Get(string id)
    {
        // ...
    }

    [Route("clients/{id}/orders")]
    public List<Order> GetByClient(string id)
    {
        // ...
    }
}

WebApiConfig.cs
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Enable CORS
        config.EnableCors();

        //config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

RouteConfig.cs
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Like this, all of the other requests work properly, but when I try to access /api/clients/1/orders, I get the error HTTP Error 404.0 - Not Found. The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
But as soon as I uncomment config.MapHttpAttributeRoutes();, I'm no longer able to access any of the requests - they all return this:
<Error>
    <Message>An error has occurred.</Message>
<ExceptionMessage>
    The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
    at System.Web.Http.Routing.RouteCollectionRoute.get_SubRoutes() at System.Web.Http.Routing.RouteCollectionRoute.GetRouteData(String virtualPathRoot, HttpRequestMessage request) at System.Web.Http.WebHost.Routing.HttpWebRoute.GetRouteData(HttpContextBase httpContext)
</StackTrace>
</Error>

What am I doing wrong here?

A:

With routing attribute [Route("clients/{id}/orders")], you should access action GetByClient() not by /api/clients/1/orders url, but with /clients/1/orders. To have an original url just fix the routing:
[Route("api/clients/{id}/orders")]
public List<Order> GetByClient(string id)

A:

Uncomment your config.MapHttpAttributeRoutes() line, then in your Global.asax file, replace this:
WebApiConfig.Register(GlobalConfiguration.Configuration);

with this:
GlobalConfiguration.Configure(WebApiConfig.Register);

You can read about it here:
Attribute Routing in ASP.NET Web API 2

