DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

INavigationManager - Using Dependency Injection in Business Controller Class

Sort:
You are not authorized to post a reply.





New Around Here





    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()))
        { }

        [...]

    }
     






    Advanced Member





      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.

      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).

      DNN partner specializing in custom, enterprise DNN development https://engagesoftware.com/showcase
      You are not authorized to post a reply.

      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:

      1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
      2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
      3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
      4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
      5. No Flaming or Trolling.
      6. No Profanity, Racism, or Prejudice.
      7. Site Moderators have the final word on approving / removing a thread or post or comment.
      8. English language posting only, please.

      Would you like to help us?

      Awesome! Simply post in the forums using the link below and we'll get you started.

      Get Involved