概述
appsettings.json:配置文件,数据库连接字符串配置信息
Program.cs:程序入口文件(里面有个Main方法)
Startup.cs:启动配置文件
依赖项:管理项目所依赖的第三方组件的安装,配置,升级
Controller:控制器
Models:实体
Views:视图
namespace Sample01.Models
{
/// <summary>
/// 2018.11.19
/// 祝雷
/// 内容实体
/// </summary>
public class Content
{
/// <summary>
/// 主键
/// </summary>
public int Id { get; set; }
/// <summary>
/// 标题
/// </summary>
public string title { get; set; }
/// <summary>
/// 内容
/// </summary>
public string content { get; set; }
/// <summary>
/// 状态 1正常 0删除
/// </summary>
public int status { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime add_time { get; set; }
/// <summary>
/// 修改时间
/// </summary>
public DateTime modify_time { get; set; }
}
}
namespace Sample01.Controllers
{
/// <summary>
/// Content控制器
/// </summary>
public class ContentController : Controller
{
/// <summary>
/// 首页显示
/// </summary>
/// <returns></returns>
public IActionResult Index()
{
var contents = new List<Content>();
for (int i = 1; i < 11; i++)
{
contents.Add(new Content { Id=i,title=$"{i}的标题",content= $"{i}的内容",status=1, add_time=DateTime.Now.AddDays(-i)});
}
return View(new ContentViewModel { Contents=contents});
}
}
}
依赖项注入DI
构造函数传参,松耦合,可测试,可替换,可维护
more:体系结构原则 - .NET | Microsoft Learn
https://www.cnblogs.com/youring2/p/10926590.html
概念我知道,但不知道怎么使用,是不是得去看看.net core的教程
生命周期
1.瞬态:
2.单例:
3.作用域:
builder.Services.AddTransient<IUserService,UserService>();
builder.Services.AddSingleton<IUserService,UserService>();
builder.Services.AddScoped<IUserService,UserService>();
???
注入方式
1.构造函数注入:
2.属性注入:
3.方法注入:
中间件
var builder= WebApplication.CreateBuilder(args);
var app= builder.Build();
app.Use(async (context, next) =>
{
await next.Invoke();//Invoke? 走下一个中间件
});
app.Run(async context =>
{
await context.Response.WriteAsync("你好你被中间件截胡了");//短路
});
app.Run();
1.静态文件中间件
app.UseStaticFiles();启用静态文件中间件
using Microsoft.Extensions.FileProviders;
var builder= WebApplication.CreateBuilder(args);
var app= builder.Build();
//app.Use(async (context, next) =>
//{
// await next.Invoke();
//});
//app.Run(async context =>
//{
// await context.Response.WriteAsync("你好你被中间件截胡了");
//});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFile")
),
RequestPath= "/StaticFiles"
});
app.Run();
创建该文件夹
2.改变静态文件访问地址(改变文件夹)
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFile")
),
RequestPath= "/StaticFiles"//https://localhost:7064/StaticFiles/1.png
});
因为缓存的原因原来的还能被打开
3.启用目录浏览
展示该站点下有哪些静态文件,服务可以访问
using Microsoft.Extensions.FileProviders;
var builder= WebApplication.CreateBuilder(args);
var app= builder.Build();
var fileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "MyStaticFile"));//1.文件路径
var requestPath = "/MyImages";//2.请求路径
//app.Use(async (context, next) =>
//{
// await next.Invoke();
//});
//app.Run(async context =>
//{
// await context.Response.WriteAsync("你好你被中间件截胡了");
//});
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFile")
),
RequestPath= requestPath ,//3
});
app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
FileProvider = fileProvider,
RequestPath = requestPath
}); //4.开启目录浏览
app.Run();
4.https重定向
app.UseHsts();
app.UseHttpsRedirection();//实现重定向 -当打开的是不安全的站点时自动跳转
5.读取配置
builder.Services.AddDirectoryBrowser();
app.MapGet("config1", (IConfiguration configuration) =>
{
return configuration["msg"] + "_" + configuration["Logging:LogLevel:Default"];
});
app.MapGet("config2", (IConfiguration configuration) =>
{
return configuration.GetValue<string>("msg");
});
app.MapGet("config3", (IConfiguration configuration) =>
{
return configuration.GetSection("msg").Value;
});
IConfiguration //configuration构造
通过路由终结点来演示读取这个配置
builder.Services.AddDirectoryBrowser();
app.MapGet("config", (IConfiguration configuration) =>
{
return configuration["msg"] + "_" + configuration["Logging:LogLevel:Default"];
});
通过GetValue方法获取
通过GetSection方法获取
5.2读取对象配置
using WebApplication5.Model;
app.MapGet("config4", (IConfiguration configuration) =>
{
return configuration.GetSection("Person").Get<Person>();
});