第一种是读取appsettings.json
新建的项目它默认是和源代码根目录在一起的,内容如下
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
在程序启动的Program.cs时就有如下代码
using coreapiauth;
using coreapiauth.TestWebApi.AuthCenter.Utility;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
所以拿到builder就可以了
if (builder.Configuration["SecurityKey"]==null|| builder.Configuration["SecurityKey"].Length==0)
{
throw new Exception("please defined key on appsettings.json [SecurityKey issuer audience] is must!");
}
在控制器中如何拿到呢?
这就需要定义构造方法了,之前默认构造方法是不带参数的,改成带参数的,不过看上去不是复写方法,和java语法不同,这种注入不需要定义什么注解它就能给你传递进来。
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
//using TestWebApi.AuthCenter.Utility;
namespace TestWebApi.AuthCenter.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
//需要注入的服务
private readonly ILogger<AuthController> _logger;
private readonly IConfiguration _configuration;
private readonly IJwtService _jwtService;
public AuthController(IConfiguration configuration,
ILogger<AuthController> logger,
IJwtService jwtService)
{
_configuration = configuration;
_logger = logger;
_jwtService = jwtService;
}
}
}
或者
···
private readonly IConfiguration _iConfiguration = null;
public ConfigurationController(IConfiguration configuration)
{
this._iConfiguration = configuration;
}
···
第二种
这种会生成一个你的程序名(.dll|.exe).config这种似乎不方便调试在源代码阶段,这个配置文件和源代码并不是在一起的,而且它默认是没有的。
using System.Configuration;
using System.Data.SqlClient;
using webapi_test.consts;
using ConfigurationManager = System.Configuration.ConfigurationManager;
namespace webapi_test.utils
{
public class ConfigUtil
{
public static void init()
{
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (cfa.AppSettings.Settings["LaunchTime"] == null)
{
cfa.AppSettings.Settings.Add("LaunchTime", "");
cfa.AppSettings.Settings.Add("DataSource", "");
cfa.AppSettings.Settings.Add("InitialCatalog", "");
cfa.AppSettings.Settings.Add("Password", "");
cfa.AppSettings.Settings.Add("UserID", "SA");
cfa.AppSettings.Settings.Add("ConnectTimeout", "15");
}
else
{
String DataSource = cfa.AppSettings.Settings["DataSource"]?.Value??"";
if (DataSource != null&&DataSource.Length>0)
{
String InitialCatalog = cfa.AppSettings.Settings["InitialCatalog"]?.Value??"";
String UserID = cfa.AppSettings.Settings["UserID"]?.Value??"";
int ConnectTimeout =ParseUtil.parseInt(cfa.AppSettings.Settings["ConnectTimeout"]?.Value ?? "15");
String Password = cfa.AppSettings.Settings["Password"]?.Value ?? "";
Consts.strConn= SercretCS. Conn = new SqlConnectionStringBuilder()
{
DataSource = DataSource,
InitialCatalog = InitialCatalog,//
UserID = UserID,
ConnectTimeout = ConnectTimeout,
Password = Password
}.ConnectionString;
}
}
cfa.AppSettings.Settings["launchTime"].Value = DateTime.Now.ToString();
cfa.Save();
}
}
}
如果写入成功内容如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="LaunchTime" value="xxx" />
<add key="DataSource" value="xxx" />
<add key="InitialCatalog" value="xxx" />
<add key="Password" value="xxx" />
<add key="UserID" value="SA" />
<add key="ConnectTimeout" value="15" />
</appSettings>
</configuration>
网友评论