简体   繁体   中英

web api versioning with default link

I have web api with two version v1 and v2, they are like folders in controllers, these folders contains controllers with same names and methods. My WebApiConfig looks like this

       config.Routes.MapHttpRoute(
            "DefaultApi", 
            "api/v{version}/{controller}/{id}",
            new {id = RouteParameter.Optional}
            );
        config.Services.Replace(typeof(IHttpControllerSelector), new HttpControllerSelector((config)));

Respectively my links looks like api/v1/custum/get?id=3 and api/v2/custum/get?id=3 , how I can do navigation in link api/custum/get?=3 at last version ie at v2/custum/get?id=3

You can set the default value for the version parameter like this

  config.Routes.MapHttpRoute(
            "DefaultApi",
            "api/{version}/{controller}/{id}",
            new
            {
                version="v2",
                id = RouteParameter.Optional
            }

Have a route config for versioned API and a fallback config. For example,

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

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

The first one maps the calls with specific version. The second one has no version in the route. In your HttpControllerSelector , try to get value for version , if it is not there for the second route config, set it to a default version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM