标签: appsettings

如何在Asp.Net MVC 6中检索AppSettings配置?

假设我使用新的DepencyInjection框架在新的ASP.Net/vNext中配置我的类和依赖项.

我该如何使用,如何获取预定义的配置设置?

    public void ConfigureServices(IServiceCollection services)
    {
        // Add Application settings to the services container.
        services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));

        // Add EF services to the services container.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        // Add Identity services to the services container.
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // Configure the options for the authentication middleware.
        // You can add options for Google, Twitter and other middleware as shown below.
        // For more information see http://go.microsoft.com/fwlink/?LinkID=532715
        services.Configure<FacebookAuthenticationOptions>(options =>
        {
            options.AppId = Configuration["Authentication:Facebook:AppId"];
            options.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
        });

        services.Configure<MicrosoftAccountAuthenticationOptions>(options …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection appsettings asp.net-core-mvc

7
推荐指数
1
解决办法
2837
查看次数

Web.config和Web.Release.config中的appSettings

试图简化<appSettings>dev与prod.

我的Web.config:

<appSettings>
  <add key="hello" value="debug" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我的Web.Release.config:

<appSettings>
  <add key="hello" value="prod" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

(均在<configuration>)

当我在调试模式下运行并运行我的MVC站点时,我可以return Content(WebConfigurationManager.AppSettings["hello"]);在我的内容中执行简单操作HomeController.Index并返回dev.如果我将模式切换到Release它仍然返回dev.我想模拟prod模式而不实际发布到prod.

c# appsettings asp.net-mvc-3

7
推荐指数
1
解决办法
3970
查看次数

如何在安装时设置应用程序设置(通过安装程序类)

我有一个具有Installer类的Visual Studio安装项目.在安装程序类中,我设置如下设置:

MessageBox.Show(Properties.Settings.Default.MySetting);

Properties.Settings.Default.MySetting = "Foo";
Properties.Settings.Default.Save();

MessageBox.Show(Properties.Settings.Default.MySetting);
Run Code Online (Sandbox Code Playgroud)

问题是,即使我知道这个代码正在执行(我正在做其他的事情),设置永远不会设置!

消息框确实表明正在设置该值,但是当我转到.config文件时,该值仍然是空白的!

任何人有任何想法和/或可能的解决方法?

c# setup-project appsettings

6
推荐指数
1
解决办法
6532
查看次数

什么是 appSettings 键有效字符?

这很奇怪。我以为我很容易在谷歌上找到这些信息,但我没有取得太大的成功。我只想知道哪些有效字符可用于 Web.config 文件的 AppSettings 部分中的键。例如:

<add key="MySpeed" value="100" />
<add key="My.Speed" value="100" />
<add key="My Speed" value="100" />
<add key="Vélocité" value="100" />
Run Code Online (Sandbox Code Playgroud)

是否允许使用上述所有键?

asp.net configuration-files appsettings

6
推荐指数
1
解决办法
2074
查看次数

在 VB.net 中,如何保存对 app.config 的更改

我的 app.config 文件中有几个 appSettings,具有默认值:

<appSettings>
  <add key="Foo" value="one"/>
  <add key="Bar" value="two"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我可以读取并将值放入文本框和组合框

我有这段代码来保存对这两个文件所做的更改,但我所做的更改不会保存到 app.config 文件本身,因此当我关闭程序并再次打开它时,这些值会恢复为默认值。

Private Sub ButtonSaveSettings_Click(sender As Object, e As EventArgs) Handles ButtonSaveSettings.Click
    Dim settings = System.Configuration.ConfigurationManager.AppSettings

    settings.Set("Foo", TextBoxFoo.Text)
    settings.Set("Bar", ComboBoxBar.SelectedItem.ToString)
End Sub
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能将更新的值保留到 app.config 文件中?

(编辑:副本上的答案不适用于 VB,并且没有解决此问题。)

vb.net app-config appsettings

6
推荐指数
1
解决办法
2万
查看次数

如何使用 C# 将 AppSettings.json 转换为高级 Azure 设置?

您是否注意到Azure升级了AppSettings Management?现在可以使用高级编辑选项一次性更新多个 AppSettings,但格式与 AppSettings.json 不同。

Azure 编辑选项

我正在寻找一种快速解决方案,将我的 AppSettings 部分转换为 Azure 高级编辑选项格式。你知道怎么做吗?

所以这:

"Simulation": {
    "ApiUrl": "YourApiUrl",
    "ApiKey": "YouApiKey",
    "Groups": [
        {
            "Name": "YourGroup",
            "Latitude": 45.50884,
            "Longitude": -73.58781,
            "Radius": 500
        }
    ],
    "Planifications": [
        {
            "GroupName": "YourGroup",
            "At": "07:00",
            "Status": 10
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

将被格式化为:

[
  {
    "Name": "Simulation:ApiUrl",
    "Value": "YourApiUrl",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:ApiKey",
    "Value": "YourApiKey",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Name",
    "Value": "YourGroup",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Latitude",
    "Value": "45.50884",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Longitude",
    "Value": …
Run Code Online (Sandbox Code Playgroud)

appsettings azure .net-core

6
推荐指数
1
解决办法
3350
查看次数

如何引用同一 appsettings.json 文件中的另一个值?

我需要在 appsettings.json 中的两个位置使用数据库连接字符串。

是否可以在 json 文件中引入公共变量或 json-path 相关引用以避免潜在的问题?

如果能在不接触 C# 代码的情况下拥有它,那就太棒了。

{
...
  "ConnectionStrings": {
    "Default": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres"
  },
  "Nlog": {
    "targets": {
      "database": {
        "type": "Database",
        "dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
        "connectionString": "Host=localhost;Database=db;Port=5432;Username=postgres;Password=postgres",
...
      }
    }
...
}
Run Code Online (Sandbox Code Playgroud)

json nlog appsettings .net-core asp.net-core

6
推荐指数
2
解决办法
4357
查看次数

如何使用 .NET Core 从控制台应用程序中的 appsettings.json 获取值?

我正在使用 .NET Core 3.1 创建一个控制台应用程序,我想要一个 appsettings json 在执行开始时加载所有环境、路径、变量...,然后从其他库类中获取值。我已经使用 appsettings json 中包含的数据创建了一个“设置”类。这是我在教程中已经看到的,但我无法获得任何价值。

//Start.cs
public class Startup
{
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
}

//Settings.cs
 public class Settings
    {
        public ConnectionStrings ConnectionStrings { get; set; }
        public Logging Logging { get; set; }
        public AppSettings AppSettings { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# startup appsettings .net-core

6
推荐指数
5
解决办法
1万
查看次数

如何访问 GitHub Actions 环境机密?

很简单的问题,我不确定我错过了什么。

\n

我有一个环境秘密:

\n

在此输入图像描述

\n

在这种情况下,“生产”环境可用于所有分支。\n我只是尝试在 appsettings 替换操作中访问 MESSAGE 变量(我尝试添加和删除存储库级别 MESSAGE 但无济于事):

\n
- uses: actions/checkout@master\n- name: Variable\xe2\x80\xafSubstitution\n  uses: microsoft/variable-substitution@v1\n  with:\n    files: DummyApi/appsettings.json\n  env:\n    Message: ${{ secrets.MESSAGE }}\n
Run Code Online (Sandbox Code Playgroud)\n

这将用 repo 级别设置替换它,但不是 env 级别设置,这根本不会替换它:

\n
- uses: actions/checkout@master\n- name: Variable\xe2\x80\xafSubstitution\n  uses: microsoft/variable-substitution@v1\n  with:\n    files: DummyApi/appsettings.json\n  env:\n    Message: ${{ env.MESSAGE }}\n
Run Code Online (Sandbox Code Playgroud)\n

简单地说 - 如何访问环境中的 MESSAGE 值?我需要手动设置环境吗?谢谢。

\n

补充信息:

\n

我真正想做的是这样的:

\n
on: [push]\njobs:\n  build-and-deploy:\n    runs-on: ubuntu-latest\n    environment: contains(\'main\', $GITHUB_REF) ? production : development\n
Run Code Online (Sandbox Code Playgroud)\n

我知道最后一行不起作用,但似乎如果我设置环境,我可以看到秘密 - 我只想重用大量部署代码而不是复制/粘贴。

\n

github appsettings github-actions

6
推荐指数
0
解决办法
1181
查看次数

如何从 appsettings.json 获取日期时间?

我有 .net core 5.0 应用程序并尝试从 appsettings.json 获取 DateTime

应用程序设置.json:

"TimeModel": {
"RestartDuration": "27.10.2021 12:30:00"
 }
Run Code Online (Sandbox Code Playgroud)

代码:

 services.Configure<TimeModel>(
            configuration.GetSection("TimeModel"));
Run Code Online (Sandbox Code Playgroud)

错误:

 System.InvalidOperationException: Failed to convert configuration value at 'TimeModel:RestartDuration' to type 'System.DateTime'.
   ---> System.FormatException: 27.10.2021 12:30:00 is not a valid value for DateTime.
   ---> System.FormatException: String '27.10.2021 12:30:00' was not recognized as a valid DateTime.
Run Code Online (Sandbox Code Playgroud)

c# datetime appsettings .net-core

6
推荐指数
1
解决办法
6041
查看次数