[Asp.Net Core] 命令行参数读取+配置多种读取

Posted 厦门德仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Asp.Net Core] 命令行参数读取+配置多种读取相关的知识,希望对你有一定的参考价值。

静态文件读取+脚本参数

一、静态文件读取

1、还原状态



拷贝项目中的WWWROOT文件夹后还是存在异常:

2、读取静态文件的中间件

Startup.cs 配置文件

            //读取静态文件的中间件
            //Nuget引入Microsoft.Extensions.FileProviders
            //System.IO
            app.UseStaticFiles(new StaticFileOptions()
            
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
            );

3、预览测试

二、脚本参数

1 、命令行参数

    新建一个FirstController
        private readonly IConfiguration _Configuration;
        public FirstController(ILogger<FirstController> logger, IConfiguration configuration, IOptions<DbConnectionOptions> options)

            _Configuration = configuration;
            _logger.LogWarning("FirstController被构造。。。");
        
        public IActionResult Index()
        
            ViewBag.ParameterInfo = _Configuration["port"];
            ViewBag.Id = _Configuration["Id"];
            return View();
        

index.html


<h1>配置文件:@ViewBag.configInfo</h1>

<h1>命令参数:@ViewBag.ParameterInfo</h1>

代码如下(示例):
Startup.cs

        public void ConfigureServices(IServiceCollection services)
        
            services.AddControllersWithViews();
            services.AddSession();

            #region 读取配置文件成实体对象类型
            services.Configure<DbConnectionOptions>(Configuration.GetSection("ConnectionStrings"));
            #endregion

            #region 读取配置文件
            Console.WriteLine($"Id:Configuration["Id"]");
            Console.WriteLine($"Name:Configuration["Name"]");
            Console.WriteLine($"TeachInfo.Id:Configuration["TeachInfo:Id"]");
            Console.WriteLine($"TeachInfo.Name:Configuration["TeachInfo:Name"]");
            Console.WriteLine($"ConnectionStrings.WriteConnection:Configuration["ConnectionStrings:WriteConnection"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList1:Configuration["ConnectionStrings:ReadConnectionList:0"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList2:Configuration["ConnectionStrings:ReadConnectionList:1"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList3:Configuration["ConnectionStrings:ReadConnectionList:2"]");
            #endregion
        

2.配置文件读取:

第一种:

通过IConfiguration索引取值;
如果遇到对象,数组;
中间使用:分割,依次读取;

代码如下(示例):

        public IActionResult Index()
        
            ViewBag.ParameterInfo = _Configuration["port"];
            ViewBag.Id = _Configuration["Id"];
      
            ViewBag.WriteConnection = _Configuration["ConnectionStrings:WriteConnection"];
            ViewBag.WriteConnection01 = _Configuration["ConnectionStrings:ReadConnectionList:0"];
            ViewBag.WriteConnection02 = _Configuration["ConnectionStrings:ReadConnectionList:1"];
            ViewBag.WriteConnection03 = _Configuration["ConnectionStrings:ReadConnectionList:2"];
            return View();
        

html

<h1>
    @ViewBag.WriteConnection
</h1>
<h1>
    @ViewBag.WriteConnection01
</h1>
<h1>
    @ViewBag.WriteConnection02
</h1>
<h1>
    @ViewBag.WriteConnection03
</h1>

测试预览

第二种:

1.定义一个和配置文件内容格式一致的类
2.在ConfigureServices 配置 services.Configure(Configuration.GetSection(“ConnectionStrings”));
3.在使用的时候可以直接通过IOptions options注入
4.获取注入的Options.Value 就是我们获取的到配置文件的一个实体类型对象
实体

    public class DbConnectionOptions
    
        public string WriteConnection  get; set; 

        public List<string> ReadConnectionList  get; set; 
    

Controller

        private readonly ILogger<FirstController> _logger;
        private readonly IConfiguration _Configuration;
        private readonly DbConnectionOptions _optionsCurrent; //与配置文件结构一样的实体
        public FirstController(ILogger<FirstController> logger, IConfiguration configuration, IOptions<DbConnectionOptions> options)
        
            _logger = logger;
            _Configuration = configuration;
            _optionsCurrent = options.Value;
            _logger.LogWarning("FirstController被构造。。。");
        
        public IActionResult Index()
        
            ViewBag.ParameterInfo = _Configuration["port"];
            ViewBag.Id = _Configuration["Id"];
            ViewBag.WriteConnection = _Configuration["ConnectionStrings:WriteConnection"];
            ViewBag.WriteConnection01 = _Configuration["ConnectionStrings:ReadConnectionList:0"];
            ViewBag.WriteConnection02 = _Configuration["ConnectionStrings:ReadConnectionList:1"];
            ViewBag.WriteConnection03 = _Configuration["ConnectionStrings:ReadConnectionList:2"];
            object strResult = Newtonsoft.Json.JsonConvert.SerializeObject(_optionsCurrent);
            return View(strResult);
        

读取配置文件成实体对象类型

        public void ConfigureServices(IServiceCollection services)
        
            services.AddControllersWithViews();
            services.AddSession();

            #region 读取配置文件成实体对象类型
            services.Configure<DbConnectionOptions>(Configuration.GetSection("ConnectionStrings"));
            #endregion
            #region 读取配置文件
            Console.WriteLine($"Id:Configuration["Id"]");
            Console.WriteLine($"Name:Configuration["Name"]");
            Console.WriteLine($"TeachInfo.Id:Configuration["TeachInfo:Id"]");
            Console.WriteLine($"TeachInfo.Name:Configuration["TeachInfo:Name"]");
            Console.WriteLine($"ConnectionStrings.WriteConnection:Configuration["ConnectionStrings:WriteConnection"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList1:Configuration["ConnectionStrings:ReadConnectionList:0"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList2:Configuration["ConnectionStrings:ReadConnectionList:1"]");
            Console.WriteLine($"ConnectionStrings.ReadConnectionList3:Configuration["ConnectionStrings:ReadConnectionList:2"]");
            #endregion
        

总结

静态文件读取+脚本参数
静态文件读取:
1.Nuget引入:Nuget引入Microsoft.Extensions.FileProviders System.IO
2.配置读取静态文件的中间件
脚本参数:
1.可以在启动dll的脚本之后放上参数,格式:–参数名称=参数值
2.控制器通过构造函数注入IConfiguration
3.可以在控制器中通过IConfiguration[参数名称]
配置文件读取:
第一种:
通过IConfiguration索引取值;
如果遇到对象,数组;
中间使用:分割,依次读取;
第二种:
1.定义一个和配置文件内容格式一致的类
2.在ConfigureServices 配置 services.Configure(Configuration.GetSection(“ConnectionStrings”));
3.在使用的时候可以直接通过IOptions options注入
4.获取注入的Options.Value 就是我们获取的到配置文件的一个实体类型对象

以上是关于[Asp.Net Core] 命令行参数读取+配置多种读取的主要内容,如果未能解决你的问题,请参考以下文章

菜鸟入门ASP.NET Core5:命令行配置Json文件配置Bind读取配置到C#实例在Core Mvc中使用Options

将数组作为命令行参数传递给 Asp.Net Core

2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」

ASP.NET Core 读取配置 IOptions 控制器未触发

2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」

ASP.NET Core