我的第一个.netcore程序

  • 创建项目

项目创建模板采用web应用程序(模型视图控制器),要用就用最新版本的ASP.NET Core3.1,注意不要勾选HTTPS配置,

一般生产环境部署时才需要HTTPS,如果不小心勾选了,后续可以launthSettings.json 文件中,把sslPort 端口号改成0即可。

  • 项目结构介绍

1、项目启动文件配置

2、网站静态文件目录

3、项目所有的依赖包和框架

4、控制器

5、Razor视图

6、项目整体配置文件

7、项目入口文件和启动类

 

 

 

 

  • 重要文件
 1     public class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             CreateHostBuilder(args).Build().Run();
 6         }
 7 
 8         public static IHostBuilder CreateHostBuilder(string[] args) =>
 9             Host.CreateDefaultBuilder(args)
10                 .ConfigureWebHostDefaults(webBuilder =>
11                 {
12                     webBuilder.UseStartup<Startup>();
13                 });
14     }
Program.cs

这个Program是程序的入口, 看起来很眼熟, 是因为asp.net core application实际就是控制台程序(console application).

它是一个调用asp.net core 相关库的console application. 

Main方法里面的内容主要是用来配置和运行程序的。

 因为我们的web程序需要一个宿主,所以CreateHostBuilder这个方法就创建了一个IHostBuilder. 而且我们还需要Web Server.

 asp.net core 自带了两种http servers, 一个是WebListener, 它只能用于windows系统, 另一个是kestrel, 它是跨平台的.

kestrel是默认的web server, 就是通过UseKestrel()这个方法来启用的.

但是我们开发的时候使用的是IIS Express, 调用UseIISIntegration()这个方法是启用IIS Express, 它作为Kestrel的Reverse Proxy server来用.

 

如果在windows服务器上部署的话, 就应该使用IIS作为Kestrel的反向代理服务器来管理和代理请求.

如果在linux上的话, 可以使用apache, nginx等等的作为kestrel的proxy server.

当然也可以单独使用kestrel作为web 服务器, 但是使用iis作为反向代理还是有很多有优点的: 例如,IIS可以过滤请求, 管理证书, 程序崩溃时自动重启等.

不过目前因为跨平台,所以使用较多的还是 nginx。

webBuilder.UseStartup<Startup>();, 这句话表示在程序启动的时候, 我们会调用Startup这个类.

Build()完之后返回一个实现了 IHost 接口的实例(IHostBuilder), 然后调用Run()就会运行Web程序, 并且阻止这个调用的线程, 直到程序关闭.

 1     public class Startup
 2     {
 3         public Startup(IConfiguration configuration)
 4         {
 5             Configuration = configuration;
 6         }
 7 
 8         public IConfiguration Configuration { get; }
 9 
10         // This method gets called by the runtime. Use this method to add services to the container.
11         public void ConfigureServices(IServiceCollection services)
12         {
13             services.AddControllersWithViews();
14         }
15 
16         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
17         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
18         {
19             if (env.IsDevelopment())
20             {
21                 app.UseDeveloperExceptionPage();
22             }
23             else
24             {
25                 app.UseExceptionHandler("/Home/Error");
26             }
27             app.UseStaticFiles();
28 
29             app.UseRouting();
30 
31             app.UseAuthorization();
32 
33             app.UseEndpoints(endpoints =>
34             {
35                 endpoints.MapControllerRoute(
36                     name: "default",
37                     pattern: "{controller=Home}/{action=Index}/{id?}");
38             });
39         }
40     }
Startup.cs

其实Startup算是程序真正的切入点,是配置服务和中间件的启动类。

Startup 默认构造函数,注入了配置项 IConfiguration。

ConfigureServices方法是用来把services(各种服务, 例如identity, ef, mvc等等包括第三方的, 或者自己写的)加入(register)到container(asp.net core的容器)中去, 并配置这些services. 这个container是用来进行dependency injection的(依赖注入). 所有注入的services(此外还包括一些框架已经注册好的services) 在以后写代码的时候, 都可以将它们注入(inject)进去. 例如上面的Configure方法的参数, app, env, loggerFactory都是注入进去的services.

Configure 方法是asp.net core程序用来具体指定如何处理每个http请求的, 例如我们可以让这个程序知道我使用mvc来处理http请求, 那就调用 app.UseEndpoints 这个方法就行,这个是一个短路中间件,表示 http 请求到了这里就不往下走了.  

 1 public void ConfigureServices(IServiceCollection services)
 2         {
 3             services.AddControllersWithViews();
 4 
 5             //services.AddDbContext<YFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EFStr")), ServiceLifetime.Singleton, ServiceLifetime.Singleton);
 6             services.AddDbContext<YFDbContext>(options =>
 7             {
 8                 options.UseSqlServer(Configuration.GetConnectionString("EFStr"));
 9 
10             }, ServiceLifetime.Singleton);
11         }
通过ConfigureServices方法注入EFCore的数据库上下文示例

 

posted @ 2020-06-01 14:49  D、boy  阅读(307)  评论(0编辑  收藏  举报