一.ASP.NET Core中的配置源:
appsettings.json,apppsettings.{Enviroment}.json,不同环境下对应不同的托管环境。
User secrets(用户机密)
Enviroment varialbles(环境变量)
Command-line arguments(命令行参数)
二.配置
{
"Logging":{
"LogLevel":{
"Default":"Warning"
}
},
"AllowedHosts":"*",
"MyKey":"appsetting.json中的MyKey的值"
}
//打印到浏览器
//配置startup
//依赖注入 低耦合 可扩展
private readonly IConfiguration _configuration;
publuc startUp(IConfiguration configuration)
{
_configuration=configuration;
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app,IHostingEnviroment env)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run((context)=>
{
context.Response.ContentType="text/plain;charset="utf-8";
var vonfigurationvalue=_configuration["MyKey"];
await context.Response.WriteAsync("Hello Word")
})
}
}
访问配置信息:
IConfiguration配置接口
三.开发环境下的appsetting.json(IISExpress)
1.在开发环境下 appsetting.Development.json会对对appsetting.json文件进行覆盖
2.一些私密信息怎么处理
创建一个私密信息secrets.json文件
{
"Mykey":"sppsetting.secrets.json.Mykey"
}
3.在lanuchSetting.json文件下配置
{
"IiisSettings":{
"windowsAuthentication":false,
"anonymousAuthentication":true,
"iisExpress":{
"applicationUrl":"http:..localhost:3290",
"sslPort":0
}
"profiles":{
"IIS Express":{
"commandName":"IISExpress",
"lanunchBrowser":true,
"enviromentVariables":{
"ASPNETCORE_ENVIROMEN":"Development",
"MyKey":"appsetting.lannchsetting.json MyKey",
},
},
"ProjectName":
{
"commandName":"Project",
"lanuchBrowser":tru,
"applicationUrl":"http://localhost:5000",
"enviromentVariables":{
"ASPNETCORE_ENVIROMENT":"Development"
}
}
}
}
}
四.使用Command Line配置源
1.dotnet run MyKey="abcdefg"
2.使用Command Line会使用代理的方式对项目进行运行,Core的项目本身可以进行自宿主。