I'm developing an API plugin on Nop3.9 and I want to make URL versioning for my API.
I've registered this route in my RouteProvider class:
public void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("Nop.Plugin.Api.Zarvan.Controllers",
"Plugins/Api/Zarvan/v{ZarvanApiVersion}/{controller}/{action}/{id}",
new { id = UrlParameter.Optional },
new[] { "Nop.Plugin.Api.Zarvan" }
);
}
As you see I need same controller action for different versions, so I made directories under my controller folder that represent the versioning like V1_1 OR V1_2 etc.
therefore my namespaces are like these for same controller and action:
Nop.Plugin.Api.Zarvan.Controllers.V1_1
Nop.Plugin.Api.Zarvan.Controllers.V1_2
I Solve this problem by overriding DefaultControllerFactory like this:
namespace Nop.Web
{
public class ZarvanApiControllerFactory : DefaultControllerFactory
{
protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
if (requestContext.RouteData.Values["ZarvanApiVersion"] != null)
{
var targetVersion = requestContext.RouteData.Values["ZarvanApiVersion"].ToString().Replace(".", "_");
requestContext.RouteData.DataTokens["namespaces"] = new[] { "Nop.Plugin.Api.Zarvan.Controllers.V" + targetVersion };
}
return base.GetControllerType(requestContext, controllerName);
}
}
}
But I,m have to put this on Nop.Web and register it in Global.asax like:
ControllerBuilder.Current.SetControllerFactory(typeof(ZarvanApiControllerFactory));
And it's works fine.
But my question is how can I override DefaultControllerFactory on my plugin structure?