1

I working on a REST MVC project. And investigating versioning for the API project.

Requests are being sent to a separate ASP.NET MVC project, and then to another separate project, client agent. Finally, client agent will flow the requests to the API project (ASP.Net Core).

The versioning is gonna sit on API project.

I have implemented a quick versioning on one of my APIs, and works fine. But the way I've done it is my issue. I have hard coded the actual version (v1.0) in the requested URL on the 'Client Agent' project. I will need to know where should I specify the required version for the API? I mean it must be from client side (javascript maybe). Please advise.

I have implemented route convention, so the route is always based on this:

"api/v{version:apiVersion}/controller"

But I still don't know, how to specify the version before it gets to my Client Agent project.

Below is my route convention class:

public class RouteConvention : IApplicationModelConvention
{
    //private readonly AttributeRouteModel _centralPrefix;

    private readonly string _versionConstraintTemplate;
    private readonly string _versionedControllerTemplate;

    public RouteConvention() //IRouteTemplateProvider routeTemplateProvider
    {
        //_centralPrefix = new AttributeRouteModel(routeTemplateProvider);

        _versionConstraintTemplate = "v{version:apiVersion}";
        _versionedControllerTemplate = $"{_versionConstraintTemplate}/[controller]";
    }   

    public void Apply(ApplicationModel application)
    {
        foreach (var applicationController in application.Controllers)
        {
            foreach (var applicationControllerSelector in applicationController.Selectors)
            {
                if (applicationControllerSelector.AttributeRouteModel != null)
                {
                    var versionedConstraintRouteModel = new AttributeRouteModel
                    {
                        Template = _versionConstraintTemplate
                    };

                    applicationControllerSelector.AttributeRouteModel =
                        AttributeRouteModel.CombineAttributeRouteModel(versionedConstraintRouteModel,
                            applicationControllerSelector.AttributeRouteModel);
                }
                else
                {
                    applicationControllerSelector.AttributeRouteModel = new AttributeRouteModel
                    {
                        Template = _versionedControllerTemplate
                    };
                }
            }
        }
    }
}

Please advise

1 Answer 1

1

The client can specify the required version in many ways. It can be part of the URL, header, query parameter etc. It all depends on where your API contract expects the client to send it. You can take a look at this ASP.NET Versioning

Coming back to the Route convention you have implemented. Looking at the URL model, it looks like you are intending to have the version specified by the client in the URL. If so, then you have to rethink your RouteConvention. Remove the versioning part of the route from the Route convention and have the version part of the route configured at the Controller level. For eg.

[Route( "v1/values" )]
public class ValuesController : Controller
{
    // GET api/v1/values
    [HttpGet]
    public string Get() 
    {
    }
}

 [Route( "v2/values" )]
 public class Values2Controller : Controller
 {
        // GET api/v2/values
        [HttpGet]
        public string Get() 
        {
        }
 }

But, again there are multiple ways of implementing versioning, you can choose what best suits your needs. ASP.NET API Versioning should get you started in the right direction.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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