1. <i id='sElHs'><tr id='sElHs'><dt id='sElHs'><q id='sElHs'><span id='sElHs'><b id='sElHs'><form id='sElHs'><ins id='sElHs'></ins><ul id='sElHs'></ul><sub id='sElHs'></sub></form><legend id='sElHs'></legend><bdo id='sElHs'><pre id='sElHs'><center id='sElHs'></center></pre></bdo></b><th id='sElHs'></th></span></q></dt></tr></i><div id='sElHs'><tfoot id='sElHs'></tfoot><dl id='sElHs'><fieldset id='sElHs'></fieldset></dl></div>
  2. <small id='sElHs'></small><noframes id='sElHs'>

    <tfoot id='sElHs'></tfoot>

        <bdo id='sElHs'></bdo><ul id='sElHs'></ul>
    1. <legend id='sElHs'><style id='sElHs'><dir id='sElHs'><q id='sElHs'></q></dir></style></legend>

      从 Startup 类访问 Configuration 对象

      Access to Configuration object from Startup class(从 Startup 类访问 Configuration 对象)

          <bdo id='OhAl3'></bdo><ul id='OhAl3'></ul>
          <tfoot id='OhAl3'></tfoot>
              <tbody id='OhAl3'></tbody>

            <small id='OhAl3'></small><noframes id='OhAl3'>

              <legend id='OhAl3'><style id='OhAl3'><dir id='OhAl3'><q id='OhAl3'></q></dir></style></legend>
            • <i id='OhAl3'><tr id='OhAl3'><dt id='OhAl3'><q id='OhAl3'><span id='OhAl3'><b id='OhAl3'><form id='OhAl3'><ins id='OhAl3'></ins><ul id='OhAl3'></ul><sub id='OhAl3'></sub></form><legend id='OhAl3'></legend><bdo id='OhAl3'><pre id='OhAl3'><center id='OhAl3'></center></pre></bdo></b><th id='OhAl3'></th></span></q></dt></tr></i><div id='OhAl3'><tfoot id='OhAl3'></tfoot><dl id='OhAl3'><fieldset id='OhAl3'></fieldset></dl></div>

                本文介绍了从 Startup 类访问 Configuration 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想从我的 ASP.NET vNext 项目的许多控制器中访问我公司的 Active Directory,并且我将域名插入到我的 config.json 文件中,因此我可以从 Configuration 类中访问它.我发现每次我想访问我的 config.json 时总是实例化一个新的 Configuration 对象很重,有没有办法通过 IConfiguration API 访问初始化到 Startup 类中的 Configuration 类?

                I would like to access to the Active Directory from my company in many controllers from my ASP.NET vNext project, and I inserted the domain name into my config.json file, so I can access it from the Configuration class. I find it heavy to always instantiate a new Configuration object at every time I want to access to my config.json, is there any way through the IConfiguration API to access to the Configuration class initialized into the Startup class ?

                推荐答案

                如何做到这一点的示例:

                An example of how you can do this:

                假设您有一个如下所示的 config.json:

                Let's assume you have a config.json like below:

                {
                    "SomeSetting1": "some value here",
                    "SomeSetting2": "some value here",
                    "SomeSetting3": "some value here",
                    "ActiveDirectory": {
                        "DomainName": "DOMAIN-NAME-HERE"
                    }
                }
                

                创建一个包含您的选项信息的 POCO 类型:

                Create a POCO type having your option information:

                public class ActiveDirectoryOptions
                {
                    public string DomainName { get; set; }
                }
                

                在 Startup.cs 中,配置服务时:

                In Startup.cs, when configuring services:

                services.Configure<ActiveDirectoryOptions>(optionsSetup =>
                {
                    //get from config.json file
                    optionsSetup.DomainName = configuration.Get("ActiveDirectory:DomainName");
                });
                

                在所有想要获取此配置设置的控制器中,执行类似...这里的选项由 DI 系统注入:

                In all controllers which want to get this config setting, do something like...Here the options is injected by the DI system:

                public class HomeController : Controller
                {
                    private readonly IOptions<ActiveDirectoryOptions> _activeDirectoryOptions;
                
                    public HomeController(IOptions<ActiveDirectoryOptions> activeDirectoryOptions)
                    {
                        _activeDirectoryOptions = activeDirectoryOptions;
                    }
                
                    public IActionResult Index()
                    {
                        string domainName = _activeDirectoryOptions.Options.DomainName;
                
                        ........
                    }
                }
                

                <小时>

                回复评论:
                我能想到几个选项:


                Responding to the comment:
                There are couple of options that I can think of:

                1. 在动作中,你可以做

                1. From within the action, you can do

                var options = HttpContext.RequestServices.GetRequiredService

                你可以有一个用FromServicesAttribute修饰的动作参数.该属性将导致从 DI 中检索参数值.示例:

                You can have a parameter to the action which is decorated with FromServicesAttribute. This attribute will cause the parameter value to be retrieved from the DI. Example:

                public IActionResult Index([FromServices] IOptions options)

                我更喜欢 #2 而不是 #1,因为在单元测试的情况下,它会为您提供所有相关部分的信息.

                I prefer #2 over #1 as in case of unit testing it gives you information on all dependent pieces.

                这篇关于从 Startup 类访问 Configuration 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                  • <i id='ETcrF'><tr id='ETcrF'><dt id='ETcrF'><q id='ETcrF'><span id='ETcrF'><b id='ETcrF'><form id='ETcrF'><ins id='ETcrF'></ins><ul id='ETcrF'></ul><sub id='ETcrF'></sub></form><legend id='ETcrF'></legend><bdo id='ETcrF'><pre id='ETcrF'><center id='ETcrF'></center></pre></bdo></b><th id='ETcrF'></th></span></q></dt></tr></i><div id='ETcrF'><tfoot id='ETcrF'></tfoot><dl id='ETcrF'><fieldset id='ETcrF'></fieldset></dl></div>
                    <tfoot id='ETcrF'></tfoot>

                          <tbody id='ETcrF'></tbody>
                      1. <small id='ETcrF'></small><noframes id='ETcrF'>

                      2. <legend id='ETcrF'><style id='ETcrF'><dir id='ETcrF'><q id='ETcrF'></q></dir></style></legend>

                        • <bdo id='ETcrF'></bdo><ul id='ETcrF'></ul>