如何从ASP.NET Core中的.json文件读取AppSettings值

编程入门 行业动态 更新时间:2024-10-26 00:24:19
本文介绍了如何从ASP.NET Core中的.json文件读取AppSettings值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在文件appsettings / Config .json中设置了我的AppSettings数据,如下所示:

I have set up my AppSettings data in file appsettings/Config .json like this:

{ "AppSettings": { "token": "1234" } }

我已经在网上搜索了如何从.json文件中读取AppSettings值的信息,但没有得到任何有用的信息。

I have searched online on how to read AppSettings values from .json file, but I could not get anything useful.

我尝试过:

var configuration = new Configuration(); var appSettings = configuration.Get("AppSettings"); // null var token = configuration.Get("token"); // null

我知道使用ASP.NET 4.0可以做到:

I know with ASP.NET 4.0 you can do this:

System.Configuration.ConfigurationManager.AppSettings["token"];

但是如何在ASP.NET Core中做到这一点?

But how do I do this in ASP.NET Core?

推荐答案

这有些曲折。我已修改此答案以使其与 ASP.NET Core 2.0 保持最新(截至2018年2月26日)。

This has had a few twists and turns. I've modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018).

此大部分取自官方文档:

要使用ASP.NET应用程序中的设置,建议仅在应用程序的中实例化配置启动类。然后,使用选项模式访问各个设置。假设我们有一个 appsettings.json 文件,如下所示:

To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. Then, use the Options pattern to access individual settings. Let's say we have an appsettings.json file that looks like this:

{ "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } }

我们有一个表示配置的POCO对象:

And we have a POCO object representing the configuration:

public class MyConfig { public string ApplicationName { get; set; } public int Version { get; set; } }

现在我们在 Startup.cs中构建配置:

public class Startup { public IConfigurationRoot Configuration { get; set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); Configuration = builder.Build(); } }

请注意, appsettings.json 将在.NET Core 2.0中默认注册。如果需要,我们还可以注册 appsettings。{Environment} .json 每个环境的配置文件。

Note that appsettings.json will be registered by default in .NET Core 2.0. We can also register an appsettings.{Environment}.json config file per environment if needed.

如果需要要将配置注入到控制器中,我们需要在运行时注册它。我们通过 Startup.ConfigureServices :

If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via Startup.ConfigureServices:

public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // Add functionality to inject IOptions<T> services.AddOptions(); // Add our Config object so it can be injected services.Configure<MyConfig>(Configuration.GetSection("MyConfig")); }

我们像这样注入它:

public class HomeController : Controller { private readonly IOptions<MyConfig> config; public HomeController(IOptions<MyConfig> config) { this.config = config; } // GET: /<controller>/ public IActionResult Index() => View(config.Value); }

完整的启动类:

public class Startup { public IConfigurationRoot Configuration { get; set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); Configuration = builder.Build(); } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // Add functionality to inject IOptions<T> services.AddOptions(); // Add our Config object so it can be injected services.Configure<MyConfig>(Configuration.GetSection("MyConfig")); } }

更多推荐

如何从ASP.NET Core中的.json文件读取AppSettings值

本文发布于:2023-11-13 10:21:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584121.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   NET   ASP   Core   AppSettings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!