开发者

ASP.NET MVC 3 Reroute to other controller and action & Restful API versioning

My original problem is I am looking开发者_运维问答 what is the best practise to do versioning in Restful API. Not much people talk about this, dont have a good answer or I can't found exactly the solution at this moment.

(1) At first I am thinking to use Tag or Branch for each version http://domain.com/API/{version}. So if new API released, I Tag it, export and publish into the respective URL but seems hard to mix the different revision of source in one web application.

(2) Then I am thinking to use this way, one controller for one version: (Just like this question Versioning of REST API Built With ASP.NET MVC 3 - 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 it need to write a route for each version.

(3) Third way I get the idea from PayPal API which is the version is not in the URL but in the POST parameter. So the URL fixed to http://domain.com/API/ but user must specify the Version parameter to have "1.0" or "2.0".

The solution for this: The (2) is ok for me, and currently I use this way but I want to mixed the (2) and (3) so I have a APIController which only have one Index action to check this Version parameter and transfer the request to the respective controller and action either APIV1Controller.{AnAction} or APIV2Controller.{AnAction}.

After Googling and Stackoverflowing about how to transfer, invoke or call another controller and action without redirection. Seems there is no good answer and good practise. Someone answer .NET MVC Call method on different controller by simply creating new instance of the controller. Suddenly I got the idea how about to reroute!

The question:

Is it possible to reroute the the other controller and action from another action without redirection and how to do that?

Or a specific question, when user request http://domain.com/API/{AnAction} with Version="2.0", how can I reroute from APIController.Index to APIV2Controller.{AnAction}?

I am not using IoC.


This can be done via routing constraints. Firstly implement 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);
    }
}

Then register routes:

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

That's all. This will do the trick.


http://coderjournal.com/2010/09/simple-rest-api-versioning-using-mef-and-mvc/

This seems to do exactly what you want, but it takes a very different approach by using a completely different set of controllers, rather than rerouting to them.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜