- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Azure 上托管了一个 ASP.NET Core 2 应用程序,并且我在 Azure 门户中为我的应用程序添加了新的应用程序设置 MyNewSetting
。
如何从 Controller 访问该设置?
我的代码如下:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSecrets>(Configuration);
services.AddSingleton<ITableRepositories, TableClientOperationsService>();
//...
我的 Controller :
public class RecordController : Controller
{
const int MyNewSetting = 7; // this one to replace with Azure Setting one
private readonly ITableRepositories repository;
public RecordController(ITableRepositories rep) {
repository = rep;
}
在这里,我可能需要添加 FromServices
注入(inject),但我不确定它是否会起作用......
编辑:
根据@dee_zg的回答,以下代码可能可以完成这项工作:
public class RecordController : Controller
{
int MyNewSetting = 7;
private readonly ITableRepositories repository;
public RecordController(ITableRepositories rep) {
repository = rep;
int myInt;
if (int.TryParse(System.Environment.GetEnvironmentVariable("MY_NEW_SETTING"),
out myInt)) {
MyNewSetting = myInt;
};
}
最佳答案
您可以选择从 AppSettings["your-key"]
集合中获取它们,也可以将其作为环境变量:Environment.GetEnvironmentVariable("your-key")
。
从那里您可以将它们映射到您的自定义 IOptions 并在需要的地方注入(inject)。
关于azure - 在 Controller 中获取 Azure AppSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032896/
我可以拥有吗?我的 app.config 中的部分包含许多设置,但也引用了 不同文件中的部分? 这将允许我保留只有开发人员应该感兴趣的配置选项,例如在主窗口上显示调试输出的选项(非常困惑但对我有用)或
我正在使用通过控制台应用程序创建的 app.config 文件,我可以使用 ConfigurationSettings.AppSettings["key1"].ToString()
我搜索了该站点,虽然我找到了一些非常有用的信息,但我无法弄清楚我的代码发生了什么。我有以下 web.config: 我
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
我正在使用 az functionapp config appsettings 自动创建我的 Azure Function 设置。我的设置之一是其中包含空格的 SQL 连接字符串。连接字符串中的空格导
工具:VS2017、ASP.NET Core 2、Entity Framework Core 2、ASP.NET Core JavaScript 服务 我正在使用以下 BuildWebHost 方法:
我正在使用 ASP.NET Core 2.1。我在 appsettings.json 中有设置我使用选项模式将它们绑定(bind)到类。我想在 appsettings.Production.json
我在 appsettings.Development.json 上有点挣扎覆盖或以其他方式与 appsettings.json 合并.我不确定如何使用 appsettings.Development.
我真的很希望能够有一种方法来获取当前使用 ConfigurationManager.AppSettings [“mysettingkey”] 来获取其设置的应用程序,以使这些设置实际上来自中央数据库而
我有一些需要从多个文件访问的字符串常量。由于这些常量的值可能会不时更改,因此我决定将它们放在 AppSettings 而不是常量类中,这样我就不必在每次更改常量时都重新编译。 有时我需要处理单个字符串
我的 App.config 中有这样的代码片段 - 在 master_AppSettings.config 文件中,我将“ApplicationDSN”设置为“ABCConnectionStri
我有一个希望非常简单的问题。 如果我有值,我想从 Web.config 的 AppSettings 中找到某个键 为了简单起见,是否可以通过指定值而不是键来找到 AppSettings 条目
我正在使用 .NET Web API(4.6 框架)编写应用 我有一个我使用的属性:[ApiExplorerSettings(IgnoreApi = true)] 从我的 Swagger 中隐藏某些
在我的办公室,我们认为我们要将 Web.Config 的 AppSettings 放入数据库中。因此,我创建了以下内容,但对代码的几个方面有一些疑问。 所以我的问题是: UTILITY 类中包含“Ca
有人能帮我理解为什么在向配置文件添加值后,我不能立即将其读入应用程序吗?我做了刷新,但这不起作用。见下文: public void AddConfig(string key_value, st
我想从名为 MyAssembly.dll.config 的程序集配置文件中检索 AppSetting key 。这是配置文件的示例: 这是检索它的代码: va
我正在为 .NET 2.0 中的配置和设置类而苦苦挣扎 如果以下内容包含在名为 app.config 的文件中 我知道我可以通过以下方式访问 appSetting // this retur
我想从名为 secrets.config 的外部配置文件中读取控制台应用程序的 appSettings 的一部分,而其余部分我想从 app.config 中读取。 目前,我已经设置好了,但它似乎没有从
我有一个配置文件 app.exe.config 和 appSettings 部分有这样的东西: app.file.config 文件有这样的东西: 我需要在运行时
我目前正在从事 asp.net core (c#) Web API 项目。 对 Web API 的每次调用都将有一个唯一的 key ,以帮助识别进行调用的用户。 每个调用者都会有一组特定的配置值与之关
我是一名优秀的程序员,十分优秀!