ASP.NET Core3基础:01. 示例项目搭建与启动顺序

一、新建示例项目可以通过dotnet cli和visual studio进行创建项目,这里使用vs进行新建这里选择ASP.NET Core Web应用程序

这里选择API,并且把HTTPS的勾去掉,点击创建。选择API是以为默认已经配置好了一些设置,方便进行测试。

为了方便学习,可以下载中文语言包,具体查看官方文档:https://docs.microsoft.com/zh-cn/dotnet/core/install/localized-intellisense二、ASP.NET Core 3的启动执行顺序首先看 ​Program.cs​ 文件,ASP.NET Core 3本质是一个控制台应用程序,Host是一个静态类,提供使用预配置默认值创建 ​IHostBuilder​实例的简便方法,​CreateDefaultBuilder​ 的参数提供了通过命令行注入配置的默认实现。​ConfigureWebHostDefaults 使用与配置默认值创建 ​IWebHostBuilder​ 类的实例。ConfigureWebHostDefaults 里面通过 ​UseStartup ​配置了​Startup​ 这个类作为启动的配置类。using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Hosting;namespace MyExample.DemoWebApi { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build( ).Run( ); } public static IHostBuilder CreateHostBuilder(string[] args) => // 使用预配置默认值初始化 HostBuilder 类的新实例 Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>( ); }); }}接着看 ​Startup.cs​ 文件,在这里可以添加我们自己的配置。构造方法中,注入了一个 ​IConfiguration​ 用于读取程序的配置信息,例如读取appsetting.json的配置信息,这里能用注入的方法是因为在 Program.cs 中已经默认为程序提供了一些默认的注入。​ConfigureServices​ 可以用来配置自定的程序服务注入,例如:EFCore的仓储注入、应用程序的配置等。一般来说不会直接在这个类下面写,而是创建一个扩展方法。​Configure ​用来配置应用程序的请求管道,API项目默认已经为程序添加了以下管道:​app.UseDeveloperExceptionPage​:显示异常的信息(发布到生成环境时不应该启用这个)​app.UseRouting​:配置应用程序的路由​app.UseAuthorization​:配置认证信息​app.UseEndpoints​:匹配路由的节点,这里配置了默认实现(RESTful格式)。也可以配置为controller/action模式:​endpoints.MapControllerRoute("default","{controller=Home}/{action=Index}/{id?}"); 。关于RESTful可以参考:http://www.ruanyifeng.com/blog/2011/09/restful.html以上仅是很小一部分,ASP.NET Core 3有非常多的默认中间件供调用,我们也可以配置自己的中间件实现,实现自定义请求的控制。using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using System.Threading;namespace MyExample.DemoWebApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers( ); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment( )) { app.UseDeveloperExceptionPage( ); } app.UseRouting( ); app.UseAuthorization( ); app.UseEndpoints(endpoints => { // 默认实现方式:RESTful endpoints.MapControllers( ); }); } }}在 ​Configure​ 方法中的中间件定义是有先后顺序区分的,默认是先定义的先执行。请求管道中的每个中间件组件负责调用管道中的下一个组件,或在适当情况下使链发生短路,官网的图:

ASP.NET Core 请求管道包含一系列请求委托,依次调用。 下图演示了这一概念。 沿黑色箭头执行。每个委托均可在下一个委托前后执行操作。 应尽早在管道中调用异常处理委托,这样它们就能捕获在管道的后期阶段发生的异常。三、简单的请求将项目启动后,请求http://localhost:5000/weatherforecast 端口可能会不同,放回的结果(经过格式化):

例子中的请求顺序可以通过控制台或者输出窗口查看

因为配置了 ​endpoints.MapControllers( ); ​,所以http://localhost:5000/weatherforecast 会找到 ​Controller/​WeatherForecastController的​Get​方法,并返回数据。

(0)

相关推荐

    Database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1]

    select ID from ac_posts where ziID =  ;