既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
LaunchSettings.json文件,仅仅只是在本地的电脑上使用。这也就是意味着,当我们发布ASP.NET Core应用程序到生产环境的时候,这个文件是不需要的。
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())
{
![img](https://img-blog.csdnimg.cn/img_convert/cd4ce51bfeeb7895fe86ee8d1bea50d4.png)
![img](https://img-blog.csdnimg.cn/img_convert/c82e9f160c8c65b4d83171a217e355b0.png)
![img](https://img-blog.csdnimg.cn/img_convert/cd05527201cb1b4c5e63027527b6aac6.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
57A-1715816718271)]
[外链图片转存中...(img-U0i7uNBq-1715816718272)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**