appsettings.json:配置文件,数据库连接字符串配置信息
Program.cs:程序入口文件(里面有个Main方法)
Startup.cs:启动配置文件
依赖项:管理项目所依赖的第三方组件的安装,配置,升级
Controller:控制器
Models:实体
Views:视图
namespace Sample01.Models
{
///
/// 2018.11.19
/// 祝雷
/// 内容实体
///
public class Content
{
///
/// 主键
///
public int Id { get; set; }
///
/// 标题
///
public string title { get; set; }
///
/// 内容
///
public string content { get; set; }
///
/// 状态 1正常 0删除
///
public int status { get; set; }
///
/// 创建时间
///
public DateTime add_time { get; set; }
///
/// 修改时间
///
public DateTime modify_time { get; set; }
}
}
namespace Sample01.Controllers
{
///
/// Content控制器
///
public class ContentController : Controller
{
///
/// 首页显示
///
///
public IActionResult Index()
{
var contents = new List();
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});
}
}
}
构造函数传参,松耦合,可测试,可替换,可维护
more:体系结构原则 - .NET | Microsoft Learn
https://www.cnblogs.com/youring2/p/10926590.html
概念我知道,但不知道怎么使用,是不是得去看看.net core的教程
生命周期
1.瞬态:
2.单例:
3.作用域:
builder.Services.AddTransient
builder.Services.AddSingleton
builder.Services.AddScoped
???
注入方式
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();
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();
创建该文件夹
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFile")
),
RequestPath= "/StaticFiles"//https://localhost:7064/StaticFiles/1.png
});
因为缓存的原因原来的还能被打开
展示该站点下有哪些静态文件,服务可以访问
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();
app.UseHsts();
app.UseHttpsRedirection();//实现重定向 -当打开的是不安全的站点时自动跳转
builder.Services.AddDirectoryBrowser();
app.MapGet("config1", (IConfiguration configuration) =>
{
return configuration["msg"] + "_" + configuration["Logging:LogLevel:Default"];
});
app.MapGet("config2", (IConfiguration configuration) =>
{
return configuration.GetValue("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方法获取
using WebApplication5.Model;
app.MapGet("config4", (IConfiguration configuration) =>
{
return configuration.GetSection("Person").Get();
});