Hi all,
I got a problem when replacing the Globals.NavigateURL() function with the new INavigationManager. What I have done:
namespace myNamespace { public partial class myControl : PortalModuleBase, IActionable { [...] protected INavigationManager NavigationManager { get; } [...] #region IActionable public ModuleActionCollection ModuleActions { get { ModuleActionCollection actions = new ModuleActionCollection(); switch (someCondition) { case [...]: [...] break; case [...]: actions.Add(GetNextActionID(), Localization.GetString("ActionName", LocalResourceFile), ModuleActionType.AddContent, string.Empty, string.Empty, NavigationManager.NavigateURL(string.Empty, new string[] { "parameter=value" }), false, SecurityAccessLevel.Edit, true, false); break; [...] } return actions; } } #endregion } }
I get a null reference exception (NavigationManager is null). What did I miss?
Happy DNNing!Michael
Michael TobischDNN★MVP
Since WebForms don't have Dependency Injection support, you have to set that property (in an MVC or Web API controller, you would add INavigationManager as a constructor parameter, and the DI support would automatically supply it). In the control's constructor, set that property to DependencyProvider.GetRequiredService(). See this WebForms example or the Use the Service Locator section of Andrew's WebForm DI blog post.
INavigationManager
DependencyProvider.GetRequiredService()
Hi Brian,
I tried this, but here I get an error: Method must have a return type.
Any idea? Do I have to install a NuGet package for that, and if yes, where do I find it? Is it this one? How can I install it from there? Or is it on the public NuGet feed (from Andrews blog: "Once DNN 9.4 is released it will be available on the public NuGet Feed."). Where is that? I did not find anything with the VS NuGet manager... :-(
Happy DNNing! Michael
Posted By Brian Dukes on 23 Jan 2020 09:39 AM That's the constructor, so you'll need to add that line to your constructor, or if you don't already have a constructor, use the name of your control's class instead of View
Stupid me! Too much trees to see the forest...
Thanks for your help.
Posted By Brian Dukes on 23 Jan 2020 09:40 AM Also, adding DNN 9.4 NuGet packages should automatically pull in DotNetNuke.Abstractions and DotNetNuke.DependencyInjection
I still have problems, I can't find any DNN packages when browsing using the NuGet Packet Manager (in VS 2019).
Anyway, I set the references to DotNetNuke.Abstractions, DotNetNuke.DependenyInjection and Microsoft.Extensions.DependencyInjection manually, but still get this error:
public MyView() { NavigationManager = DependencyProvider.GetRequiredService<INavigationManager>(); }
'IServiceProvider' does not contain a definition for 'GetRequiredService' and no accessible extension method 'GetRequiredService' accepting a first argument of type 'IServiceProvider' could be found (are you missing a using directive or an assembly reference?)
Any ideas?
GetRequiredService is an extension method, so you'll need to add using Microsoft.Extensions.DependencyInjection;.
GetRequiredService
using Microsoft.Extensions.DependencyInjection;
Here's the DotNetNuke.Core NuGet package, which depends on DotNetNuke.DependencyInjection. In the NuGet dialog, is the Package source drop down mistakenly set to a custom feed instead of All or nuget.org?
[...] using DotNetNuke.DependencyInjection; [...] using Microsoft.Extensions.DependencyInjection; [...]
are both set, but appear in gray (not used) in the source code.
There are also references to the two DLLs in the bin folder, DotNetNuke.DependencyInjection (v 9.4.4.0) and Microsoft.Extensions.DependencyInjection (v 2.1.1.0).
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.