Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core netcoreapp1.1中配置文件的最佳实践_Asp.net Core_Asp.net Core Mvc_Asp.net Core 1.1 - Fatal编程技术网

Asp.net core netcoreapp1.1中配置文件的最佳实践

Asp.net core netcoreapp1.1中配置文件的最佳实践,asp.net-core,asp.net-core-mvc,asp.net-core-1.1,Asp.net Core,Asp.net Core Mvc,Asp.net Core 1.1,我已在my Startup.cs中添加了对appsettings.json文件的访问权限,作为框架服务: public IConfigurationRoot Configuration { get; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFi

我已在my Startup.cs中添加了对appsettings.json文件的访问权限,作为框架服务:

public IConfigurationRoot Configuration { get; }

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();
}

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.Configure<AppConfig>(Configuration);
    services.AddMvc();
}
在.NET Framework的早期版本中,如果我需要从“SomeStaticClass”访问配置文件,我会在需要访问web.config的任何类中使用ConfigurationManager


在netcoreapp1.1中,正确的方法是什么?类似ConfigurationManager的方法或依赖项注入方法都适合我。

我认为这个问题更多的是关于如何从静态类获取上下文变量。我认为这将实现您想要的,但我不确定您为什么想要一个静态类,或者您正试图用它做什么(参见XY问题)


我所做的是创建以下类:

public static class Configuration
{
    private static IConfiguration _configuration;

    static Configuration()
    {
        var builder = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        _configuration = builder.Build();
    }

    public static string GetValue(string key)
    {
        return _configuration.GetValue<string>(key, null);
    }

    public static string GetValue(string section, string key)
    {
        return _configuration.GetSection(section).GetValue<string>(key);
    }
}
公共静态类配置
{
专用静态IConfiguration\u配置;
静态配置()
{
var builder=new ConfigurationBuilder()
.SetBasePath(目录.GetCurrentDirectory())
.AddJsonFile(“appsettings.json”);
_configuration=builder.Build();
}
公共静态字符串GetValue(字符串键)
{
返回_configuration.GetValue(key,null);
}
公共静态字符串GetValue(字符串部分,字符串键)
{
返回_configuration.GetSection(section).GetValue(key);
}
}

但是,它没有使用Startup.cs中使用IHostingEnvironment参数的环境逻辑。

好的,我想我应该创建一个类,公开配置文件作为框架的服务,这样我就可以在任何地方使用它了,想法是从ControllerRight之外的静态类访问appsettings.json,我更新了答案。您可以使用读取启动目录的根目录。\n请阅读依赖项注入的工作原理。您可以在任何地方注入
IOptions
(几乎)
public IActionResult AnyAction() {
  SomeStaticClass.SomeMethod(_appConfig.var1, _appConfig.var2, _appConfig.var3...)

 //or always have to pass the _appConfig reference

  SomeStaticClass.SomeMethod(_appConfig)
}
   public class HomeController : Controller
    {
        private readonly AppConfig _appConfig;

        public HomeController(IOptions<AppConfig> appConfig, ConfigContext configContext)
        {
            _appConfig = appConfig.Value;
            SomeStaticClass.SomeStaticMember = appConfig.Value
        }
        public IActionResult AnyAction() {
            SomeStaticClass.SomeMethod(); //The get the value you want from within
        }
    }
string fileContents = string.Empty;
using (StreamReader file = File.OpenText(@".\appsettings.json"))
{
    fileContents = file.ReadAllLines();
}
configs= JsonConvert.DeserializeObject(fileContents );
public static class Configuration
{
    private static IConfiguration _configuration;

    static Configuration()
    {
        var builder = new ConfigurationBuilder()
         .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        _configuration = builder.Build();
    }

    public static string GetValue(string key)
    {
        return _configuration.GetValue<string>(key, null);
    }

    public static string GetValue(string section, string key)
    {
        return _configuration.GetSection(section).GetValue<string>(key);
    }
}