asp.netcore 3.1 program、Startup 类详解
Program类
public class Program
{
/// <summary>
/// 应用程序入口
/// 1.asp.netcore 本质上是控制台程序
/// </summary>
/// <param name="args"></param>
public static void Main(string[] args)
{
//执行完build之后,就从控制台程序变成了asp.netcore
CreateHostBuilder(args).Build().Run();
} public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Startup 类
public class Startup
{
private readonly IConfiguration _configuration;
/// <summary>
/// 通过构造函数注入
/// </summary>
/// <param name="configuration"></param>
public Startup(IConfiguration configuration)
{
_configuration = configuration;
var aa = _configuration["FyyAspnetcore:Name"];// 获取 appsettings.json文件的数据
} // This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
//运行时同通过约定来调用 这个类 的两个方法。先调用 ConfigureServices、再调用 Configure.
/// <summary>
/// 负责依赖注入配置
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
/*DI的优点:
* 1.解耦,没有强依赖,Controller与具体的服务类解耦,
* 2.利于单元测试
* 3.不需要了解具体的服务类:Controller 不需要了解服务类及其工作细节。
* 4.也不需要管理服务类的生命周期:Controller 不需要管理服务类的生命周期,生命周期交给IOC容器DI来控制。
*/ //2.当IClock 被请求时,IOC容器会返回一个CnClock的实例。
services.AddSingleton<IClock, CnClock>();//AddSingleton 表示所注册的服务周期 是整个应用程序生存期间,整个应用程序只有一个实例,只有应用程序停止,才会被销毁。
//services.AddScoped<IClock, USClock>;//所注册的服务的生命周期是整个请求期间。一次web请求产生一个实例,web请求处理完的时候,生命周期就结束了。
//services.AddTransient<IClock, USClock>;//所注册的服务的生命周期是暂时的,服务每次没请求的时候,都会实例化一个对象。 //services.AddControllers();//注册webapi的服务 //services.AddControllersWithViews();//注册mvc的服务 //将配置文件的Json,映射到类中。(配置到类中,配置依赖注入)
services.Configure<FyyAspnetcore>(_configuration.GetSection("FyyAspnetcore"));
} /// <summary>
/// Development环境变量时,走这个方法。可以针对不同的环境,使用不同的方法。
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
//public void ConfigureDevelopment(IApplicationBuilder app, IWebHostEnvironment env)
//{ //} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// <summary>
/// 负责中间件,配置了asp.netcore http请求的管道。http请求从管道进来,处理完之后再从管道回去。如果管道什么都没有,请求进来,再回去,就什么都不会发生。
/// 放在管道中能处理请求的东西,就是中间件(middleware)。
/// </summary>
/// <param name="app">是一个服务,通过依赖注入的方式注入进来,注入的是服务的接口</param>
/// <param name="env">依赖注入</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//判断环境变量
//env.IsEnvironment("OK"); 判断自定义环境变量
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();//开发模式下插入到管道的第一个中间件。
} // app.UseAuthentication();//使用授权中间件 app.UseHttpsRedirection();//https 重定向中间件,强制客户端使用ssl协议。 //如果需要使用html、js、css 等静态文件,就需要使用 UseStaticFiles 中间件,如mvc。
//如果不使用这个中间件,客户端就访问不了这些静态文件。
app.UseStaticFiles(); app.UseRouting();//路由中间件。会检查在应用中已经注册的端点。 app.UseEndpoints(endpoints => //注册端点,端点就是进来的http请求的url的结尾那部分。这部分由端点中间件处理。
{
endpoints.MapGet("/", async context =>// 以/结尾的url,映射到下面的表达式中。
{
await context.Response.WriteAsync("Hello World!");
}); //MVC的路由端点,路由模板,路由表的形式
//endpoints.MapControllerRoute(
// name:"default",
// pattern:"{controller=Home}/{action=Index}/{id?}"
// ); //使用这种方式,可以在controller或action上添加路由,不需要添加路由表。
// endpoints.MapControllers();
});
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"FyyAspnetcore": {
"Name": "Fengyinyong",
"Age":
}
}
asp.netcore 3.1 program、Startup 类详解的更多相关文章
- [转帖]ASP.NET Core 中间件(Middleware)详解
ASP.NET Core 中间件(Middleware)详解 本文为官方文档译文,官方文档现已非机器翻译 https://docs.microsoft.com/zh-cn/aspnet/core/ ...
- Asp.Net MVC学习总结之过滤器详解(转载)
来源:http://www.php.cn/csharp-article-359736.html 一.过滤器简介 1.1.理解什么是过滤器 1.过滤器(Filters)就是向请求处理管道中注入额外的 ...
- java之StringBuffer类详解
StringBuffer 线程安全的可变字符序列. StringBuffer源码分析(JDK1.6): public final class StringBuffer extends Abstract ...
- ASP.NET连接Oracle数据库的步骤详解(转)
ASP.NET连接Oracle数据库的步骤详解 本文我们主要介绍了ASP.NET连接Oracle数据库的步骤及每个步骤需要进行的设置,希望能够对您有所帮助. 在用ASP.NET开发应用程序时, ...
- java之AbstractStringBuilder类详解
目录 AbstractStringBuilder类 字段 构造器 方法 public abstract String toString() 扩充容量 void expandCapacity(in ...
- java之StringBuilder类详解
StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...
- Java String类详解
Java String类详解 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时候,我们对它既熟悉又陌生. 类结构: public final ...
- QAction类详解:
先贴一段描述:Qt文档原文: Detailed Description The QAction class provides an abstract user interface action tha ...
- JAVAEE学习——struts2_01:简介、搭建、架构、配置、action类详解和练习:客户列表
一.struts2是什么 1.概念 2.struts2使用优势以及历史 二.搭建struts2框架 1.导包 (解压缩)struts2-blank.war就会看到 2.书写Action类 public ...
随机推荐
- java 之 实例方法和类方法
类方法:使用static修饰(静态方法),属于整个类的,不是属于某个实例的,只能处理static域或调用static方法: 实例方法:属于对象的方法,由对象来调用. 判断类方法,类方法的前面有stat ...
- Ethical Hacking - POST EXPLOITATION(4)
PIVOTING Use the hacked device as a pivot. Try to gain access to other devices in the network. Tool: ...
- Python Hacking Tools - Password Sniffing
Password Sniffing with Scapy 1. Download and install the Scapy first. pip install scapy https://scap ...
- Email正则表达式验证
在做邮箱验证的时候,进行简单的整理: string emailStr = @"/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3 ...
- Electron-vue 项目搭建
Electron 应用技术体系推荐 目录结构 demo(项目名称) ├─ .electron-vue(webpack配置文件) │ └─ build.js(生产环境构建代码) | └─ dev-cl ...
- nginx里的变量,实现简单过滤。
1,nginx内置变量 nginx 有很多内置变量可以进行简单的过滤. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...
- linux的/etc/passwd、/etc/shadow、/etc/group和/etc/gshadow
1./etc/passwd 存储用户信息 [root@oldboy ~]# head /etc/passwd root:x:::root:/root:/bin/bash bin:x:::bin:/b ...
- 一个简单的Android小实例分享,包含recycleView与recyclerView嵌套
先上图: 1.首页 2.第二页 3.第三页 项目目录: 代码不多,本人太懒,就不贴了 项目地址:
- 【评价指标】详解F1-score与多分类MacroF1&MicroF1
文章来自:一个宝藏微信公众号[机器学习炼丹术] 基本概念 首先,要背住的几个概念就是:accuracy,precision,recal, TP,FP,TN,FN TP:true positive.预测 ...
- Java容器学习之ArrayList
一.概述 ArrayList是java中十分常用的集合类,继承于AbstractList,并实现了List.RandomAccess.Cloneable和Serializable接口.ArrayLis ...