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