4

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 
        } 
    );

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.