ASP.NET MVC 3 Redirection to another controller and versioning of the Action & Restful API

My initial problem . I look at what is the best practice for version control in the Restful API . Not many people talk about this, do not have a good answer, or I can’t find the exact solution at the moment.

(1) First, I think of using Tagor Branchfor each version of http://domain.com/API/ {version}. Therefore, if a new API is released, I Tagexport and publish it in the corresponding URL, but it seems difficult to mix a different version of the source in one web application.

(2) Then I think to use this method, one controller for one version: (Just like this question. REST API versions built into ASP.NET MVC 3 are best practices )

http://domain.com/API/1.0/{AnAction} => will go to APIV1Controller.{AnAction}

http://domain.com/API/2.0/{AnAction} => will go to APIV2Controller.{AnAction}

but he needs to write a route for each version.

(3) Thirdly, I get an idea from the PayPal API, the version of which is not specified in the URL, but in the POST parameter. Therefore, the URL is bound to http://domain.com/API/, but the user must specify a parameter Versionfor "1.0"or "2.0".

The solution for this: (2) is suitable for me, and I'm currently using this method, but I want to mix (2) and (3), so I have an APIController that has only one index action to test this Versionand pass the request to the appropriate controller and the action is either APIV1Controller. {AnAction}, or APIV2Controller. {AnAction}.

Googling Stackoverflow , , . , . - .NET MVC Call , . , reroute!

:

, reroute ?

, http://domain.com/API/{AnAction} Version="2.0", reroute APIController.Index APIV2Controller.{AnAction}?

IoC.

+3
2

. IRouteConstraint:

public class RequestParameterConstraint : IRouteConstraint
{
    public string ParameterName { get; private set; }
    public string ParameterValue { get; private set; }

    public RequestParameterConstraint(string parameter, string value)
    {
        ParameterName = parameter;
        ParameterValue = value;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
        RouteValueDictionary values, RouteDirection routeDirection)
    {
        var value = httpContext.Request[ParameterName] ?? "";
        return value.Equals(ParameterValue);
    }
}

:

routes.MapRoute(
    "Version10",
    "API/{action}/{id}",
    new { controller = "APIV1", action = "Index", id = UrlParameter.Optional },
    new { header = new RequestParameterConstraint("Version", "1.0") }
    );

routes.MapRoute(
    "Version20",
    "API/{action}/{id}",
    new { controller = "APIV2", action = "Index", id = UrlParameter.Optional },
    new { header = new RequestParameterConstraint("Version", "2.0") }
    );

. .

+1

All Articles