网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
launchSettings.json配置说明:
- launchBrowser :一个布尔类型的开关,表示应用程序的时候是否自动启动浏览器
- launchUrl:如果launchBrowser被设置为true,浏览器采用的初始化路径通过该属性进行设置。
- environmentVariables:该属性用来设置环境变量。ASP.NET Core应用中正是利用这样一个环境变量来表示当前的部署环境。多环境的配置可以通过ASPNETCORE_ENVIRONMENT切换。
- commandName:启动当前应用程序的命令类型,有效的选项包括IIS、IISExpress和Project,前三个选项分别表示采用IIS、IISExpress和指定的可执行文件(.exe)来启动应用程序。如果我们使用dotnet run命令来启动程序,对应Profile的启动命名名称应该设置为Project。
- applicationUrl:应用程序采用的URL列表,多个URL之间采用分号(“;”)进行分隔。
LaunchSettings.json配置案例:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:20458",
"sslPort": 44337
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5289",
"environmentVariables": {
"ASPNETCORE\_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7002;http://localhost:5289",
"environmentVariables": {
"ASPNETCORE\_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE\_ENVIRONMENT": "Development"
}
}
}
}
2.appsettings.json
2.1 说明
首先在 ASP .NET Core 项目当中添加一个appsettings.json(默认都有该文件)文件,可以包含如下两个文件:
- appsettings.Development.json:开发环境
- appsettings.Production.json:生产环境
在appsettings.json里也可以修改默认的端口,主要是配置Kestrel节点下终结点的默认url,片段代码如下:
"Kestrel":{
"Endpoints": {
"Https": {
"Url": "https://\*:9001"
},
"Http": {
"Url": "http://\*:9000"
}
}
}
2.2 读取
1、直接读取
var Default = builder.Configuration["Logging:Loglevel:Default"];
2、模型绑定
定义模型
public class SectionModel
{
//绑定时只会绑定公有属性,不会绑定字段
public const string SectionName = "Position";
public string Title { get; set; }
public string Name { get; set; }
}
设置配置文件
"Position":{
"Title":"Editor",
"Name":"Jos"
}
绑定
var model = new SectionModel();
builder.Configuration.GetSection(SectionModel.SectionName).Bind(model);//这样就拿到了配置
//当然也可以这么写
builder.Configuration.GetSection(SectionModel.SectionName).Get<SectionModel>();
3、IOptions使用
配置
builder.Services.Configure<SectionModel>(builder.Configuration.GetSection(SectionModel.SectionName));
使用
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Reflection;
namespace WebApiTest.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly IOptions<SectionModel> _model;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IOptions<SectionModel> model)
{
_logger = logger;
_model = model;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
var Title = _model.Value.Title;
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
如果要及时响应修改,则将上述的IOptions改为IOptionsSnapshot。
4、GetValue
var number = builder.Configuration.GetValue<string>("RedisUrl", "99");//找不到的话默认99
5、GetSection
var selection = builder.Configuration.GetSection("Position");
if (selection.Exists())
{
var children = selection.GetChildren();
foreach (var subSection in children)
{
//subSection.Key selection[key]
![img](https://img-blog.csdnimg.cn/img_convert/b3b8e0a63c0d926c67dc9e24d2ce76fb.png)
![img](https://img-blog.csdnimg.cn/img_convert/0ba1ac2aae71ac998e1ff278d9162e82.png)
![img](https://img-blog.csdnimg.cn/img_convert/1e5d486e0114b547b56017250404ac59.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
MSr-1715655123343)]
[外链图片转存中...(img-nrVJDGju-1715655123343)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**