在 mvc6 中全局使用 IConfiguration [英] Using IConfiguration globally in mvc6

查看:23
本文介绍了在 mvc6 中全局使用 IConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能已经盯着这个很久了,但最近几天我已经跳入 MVC6 for asp.net,虽然我真的很喜欢这个,但我似乎找不到一种方便的方法来访问在Start.cs中用

I have Probably been staring at this for to long, but I have jumped into MVC6 for asp.net for the last few days, while I am really enjoying this, I can't seem to find a convenient way to access the Configuration after it is defined in Start.cs with

Configuration = new Configuration()
    .AddJsonFile("config.json")
    ...

那么,我需要将它添加到 DI 中还是它已经存在?或者我应该在需要使用它时创建一个新实例,因为可以创建不同的配置(例如 IIdentityMessageService)创建一个 sendgrid.json 并将其加载到服务本身中?

So, do I need to add it to the DI or is it already there? Or should I create a new instance whenever I need to use it, as it is possible to create different configurations (such as for IIdentityMessageService) create a sendgrid.json and load it within the Service itself?

可能有一个非常简单的解决方案,但就像我说的,我已经研究了好几天了.

There is probably a very easy solution, but like I said I have looking at this for days now.

推荐答案

只加载 Startup.cs 中的配置.如果您稍后在其他地方需要它们,您可以将值加载到适当的 POCO 中并在 DI 中注册它们,以便您可以将它们注入到您需要的地方.这允许您以对您的应用程序有意义的方式在不同的文件和不同的 POCO 中组织您的配置.依赖注入中已经内置了对此的支持.您可以这样做:

Only do the loading of Configurations in your Startup.cs. If you need them elsewhere later you could load the values into appropriate POCOs and register them in the DI so you can inject them where you need them. This allows you to organize your configuration in different files, and in different POCOs in a way that makes sense to your application. There is already built in support for this in the dependency injection. Here is how you would do it:

用于放置配置的 POCO:

A POCO to put your configuration in:

public class SomeOptions
{
    public string EndpointUrl { get; set; }
}

您的 Startup.cs 将配置加载到 POCO 中并在 DI 中注册.

Your Startup.cs loads the configuration into the POCO and registers it in the DI.

public class Startup
{
    public Startup()
    {
        Configuration = new Configuration()
                    .AddJsonFile("Config.json")
                    .AddEnvironmentVariables();
    }

    public IConfiguration Configuration { get; set; }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<SomeOptions>(options => 
            options.EndpointUrl = Configuration.Get("EndpointUrl"));

        services.AddMvc();
    }
}

然后在您的控制器中通过依赖注入获取您在 Startup.cs 中创建的配置 POCO,如下所示:

Then in your controller get the configuration POCO you created in the Startup.cs through dependency injection like this:

public class SomeController
{
    private string _endpointUrl;
    public SomeController(IOptions<SomeOptions> options)
    {
        _endpointUrl = options.Options.EndpointUrl;
    }
}

使用 1.0.0-beta1 版本的 aspnet5 进行测试.

Tested with 1.0.0-beta1 builds of aspnet5.

有关详细信息,请参阅ASP.Net 5 配置的基础.

For more information see The fundamentals of ASP.Net 5 Configuration.

这篇关于在 mvc6 中全局使用 IConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆