Reputation: 9645
I want to declare multiple versions of an API inside different controllers in ASP.NET MVC. Is it possible to do something like this?
routes.MapRoute(
"Default", // Route name
"API/{version}/{action}/{id}",
new {
controller = "APIv{version}",
action = "Index",
id = UrlParameter.Optional
}
);
OR
routes.MapRoute(
"Default", // Route name
"API/{version}/{action}/{id}",
new {
controller = "APIv" + version,
action = "Index",
id = UrlParameter.Optional
}
);
Upvotes: 4
Views: 198
Reputation: 3563
I've implemented it by moving different api versions into different areas
Version1:
public class AreaRegistration : PortableAreaRegistration
{
public override string AreaName
{
get { return "Api.v1"; }
}
public override void RegisterArea(System.Web.Mvc.AreaRegistrationContext context)
{
context.MapHttpRoute(
name: "Api.v1_Default",
routeTemplate: "api/v1/{company}/{action}",
defaults: new { controller = "Company", action = "Employees" }
);
}
}
Version1.1:
public class AreaRegistration : PortableAreaRegistration
{
public override string AreaName
{
get { return "Api.v1.1"; }
}
public override void RegisterArea(System.Web.Mvc.AreaRegistrationContext context)
{
context.MapHttpRoute(
name: "Api.v1_1_Default",
routeTemplate: "api/v1.1/{company}/{action}",
defaults: new { controller = "Company", action = "Employees" }
);
}
}
Please note PortableAreaRegistration from MvcContrib is used.
Upvotes: 4