[英]Accessing .config file inside a DotNetNuke Module
I have implemented a DNN module that uses a user-defined control (this user-defined control has implemented GoogleAnalytics tracking for our own use). 我已经实现了一个使用用户定义控件的DNN模块(这个用户定义的控件已经实现了GoogleAnalytics跟踪供我们自己使用)。
The control needs to access a config file in order to grab the configuration settings of our google analytics account. 控件需要访问配置文件才能获取我们的Google Analytics(分析)帐户的配置设置。 For example, the account code (UA-dddddd-dd), etc. 例如,帐户代码(UA-dddddd-dd)等。
My question: Since I'm installing the module on DotNetNuke, where is the proper location to put the config file at OR what config file would it be accessing by default? 我的问题:由于我在DotNetNuke上安装模块,将配置文件放在OR的正确位置在哪里,默认情况下它将访问哪个配置文件?
The piece of code in the control that attempts to access the config file is provided below: 控件中尝试访问配置文件的代码段如下所示:
string path = Assembly.GetExecutingAssembly().CodeBase;
FrameworkConfig.instance = new FrameworkConfig(ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(path + ".config")));
note: FrameworkConfig
is a wrapper class for System.Configuration
object. 注意: FrameworkConfig
是System.Configuration
对象的包装类。
In short, I need to know what does Assembly.GetExecutingAssembly().CodeBase
return when called through a DNN module? 简而言之,我需要知道当通过DNN模块调用时, Assembly.GetExecutingAssembly().CodeBase
返回什么? other alternatives? 其他选择?
If you add keys to the appsettings section of the DNN site's web.config, you can retrieve them using: 如果您将密钥添加到DNN站点的web.config的appsettings部分,您可以使用以下命令检索它们:
DotNetNuke.Common.Utilities.Config.GetSetting("settingname");
However, it is a better practice, and makes sense in this case to create a Settings
control in your module and to store the information there. 但是,这是一种更好的做法,在这种情况下有意义的是在模块中创建一个Settings
控件并在那里存储信息。
You do this by creating an ascx control that inherits from DotNetNuke.Entities.Modules.ModuleSettingsBase
and stores settings in the base Settings Dictionary for you module. 您可以通过创建一个继承自DotNetNuke.Entities.Modules.ModuleSettingsBase
的ascx控件并将设置存储在您的模块的基本设置字典中来完成此操作。
Your other module view controls can access the settings via a similar Dictionary
object called Settings
that is part of the ModuleUserControlBase
class. 您的其他模块视图控件可以通过名为Settings
的类似Dictionary
对象访问设置,该对象是ModuleUserControlBase
类的一部分。
For an example of how to use module settings, check out this DNN7 tutorial video and article . 有关如何使用模块设置的示例,请查看此DNN7教程视频和文章 。 A sample project with all the code is in the Related Files section of the article. 包含所有代码的示例项目位于本文的“相关文件”部分。
I found the GetSetting method returned null. 我发现GetSetting方法返回null。 It's probably not best practice but this has worked great for me in the past. 这可能不是最好的做法,但这对我来说过去很有用。
using System.Configuration;
var slug = ConfigurationManager.AppSettings["EE.MandrillTemplateSlug"];
With app setting such as: 使用应用程序设置,例如:
<add key="EE.MandrillTemplateSlug" value="newsletter" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.