sanju.dahal741 wrote:With Nop4.6 you can implement it like this
1. Add your settings in appSettings. For example, you have 2 settings, Setting1 and Setting2
"CustomConfig": {
"Setting1": "String value",
"Setting2": true
}
2. Create a new CustomConfig class
public class CustomConfig : IConfig
{
/// <summary>
/// Gets or sets a value for custom setting1
/// </summary>
public string Setting1 { get; private set; }
/// <summary>
/// Gets or sets a value for custom setting2
/// </summary>
public bool Setting2 { get; private set; }
}
3. Inject the AppSettings at your constructor and access the setting
public partial class MyController : BasePublicController
{
private readonly AppSettings _appSettings;
public MyController(AppSettings appSettings)
{
_appSettings = appSettings;
}
public virtual IActionResult Index()
{
string setting1 = _appSettings.Get<CustomConfig>().Setting1;
bool setting2 = _appSettings.Get<CustomConfig>().Setting2;
return View();
}
}
Right. This is how
IConfig settings are supposed to work. But at the moment this approach only works for
IConfig in the core, not in plugins. We are looking for a solution, details
here.
For now, you can register such settings directly in the plugin, so place this code in plugin
INopStartup.ConfigureServices():
var config = new CustomConfig();
configuration.GetSection(config.Name).Bind(config, options => options.BindNonPublicProperties = true);
AppSettingsHelper.SaveAppSettings(new List<IConfig> { config }, CommonHelper.DefaultFileProvider, false);