I am reading about Attribute Routing in Web API 2
from here
The article says,
Here are some other patterns that attribute routing makes easy.
API versioning
In this example, “/api/v1/products” would be routed to a different controller than “/api/v2/products”.
/api/v1/products
/api/v2/products
How come?
EDIT: I would do this in Normal Routing:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v2/products",
defaults: new { controller = V2_Products }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/products",
defaults: new { controller = V1_Products }
);
}
}
Could anyone explain me how to do this in Attribute Routing
way ? And how come using Attribute routing
is easier and convenient for this example (according to the article) ?
There are many ways to implement versionning with attribute routing ; A really basic way is to use RoutePrefix attribute for each version of your ApiController
/v1/products
goes to the first version of/v2/products
goes to the second one.Another simple way is configuring your route as api/{folder}/{controller}/{action} where in to folder you can give name as V1 or V2.
A good way can be implementing your own Controller selector. You can use this link for more information.
The interface that Web API uses to select a controller is
IHttpControllerSelector
. The important method on this interface is SelectController, which selects a controller for a given HttpRequestMessage.You can do it by overriding DefaultHttpControllerSelector
there you override method to selectcontroller
Then you are adding routes webapiconfig
and register controller selector in webapiconfig
So from now if you name controller ProductsV1Controller it will reffer /api/v1/products. Also please note that my example also support routes without version so if v1 is not found it will try to check if ProductsController exists
PS. Code is update one bug was there :(