I need to generate portal URLs from within an API call to a web forms module. So I need an INavigationManager instance in my API controller. I cannot do that since both Web Forms business controllers do not have dependency injection and I cannot instantiate a new NavigationManager class (it's marked internal).
Is there an alternative to the below?
public class FormsController : DnnApiController {
private INavigationManager _navigationManager; public FormsController(INavigationManager navigationManager) { _navigationManager = navigationManager; }
public FormsController() : this(new DotNetNuke.Common.NavigationManager(new PortalController())) { }
[...]
}
We do not yet support constructor injection within the business controller class (we need to wait until version 10 because implementing that will be a breaking change, affecting the lifetime of the created instances).
We do support constructor injection for Web API controllers.
In this example scenario, are you using a Web API controller (i.e. DnnApiController) as the business controller class? I would recommend separating those two ideas if you can into two separate classes.
DnnApiController
For now, if you need to access something from the dependency injection container and the framework doesn't provide easy access to it, you have two options. The first option is that you can continue to use the deprecated API until a newer API is available (i.e. use Globals.NavigateURL for now). The second option is that, if you are using your API in a context where you are guaranteed to have an HTTP context (e.g. not in a scheduled task, not in search indexing, not in import/export, etc.), you can access the dependency injection scope from the HTTP context (see example code). (And, I suppose, a third option would be to create your own class that implements the INavigationManager interface, though ultimately you'll probably want to fall back to Globals.NavigateURL for the implementation).
Globals.NavigateURL
INavigationManager
These Forums are dedicated to the discussion of DNN Platform.
For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:
Awesome! Simply post in the forums using the link below and we'll get you started.