I want to use URL-Based versioning for my controller. best solution that I found was code below. I'm looking for better solution for this. I tried Constrain It didn't work maybe i did some thing wrong my only concern is using controller with same name in different namespace...?! I userd string merging to creating wanted type. probably it is not a good idea. Please send good reference for this topic if you know any.....?
public class ControllerVersioning : DefaultHttpControllerSelector
{
private HttpConfiguration _config;
public ControllerVersioning(HttpConfiguration config)
: base(config)
{
_config = config;
}
public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var routeData = request.GetRouteData();
var controllerName = routeData.Values["controller"].ToString();
controllerName = char.ToUpper(controllerName[0]) + controllerName.Substring(1);
var versionName = routeData.Values["version"].ToString();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
controllerDescriptor.Configuration = _config;
controllerDescriptor.ControllerName = controllerName;
string s = "ngolforoushan.Web.Api.Controllers.V" + versionName + "." + controllerName + "Controller";
Type t=Type.GetType(s);
controllerDescriptor.ControllerType = t;
return controllerDescriptor;
}
}