机械网站建设营销,单页面优化的重点,代理记账公司注册,东道设计公司官网招聘点击蓝字关注我们课程链接#xff1a;http://video.jessetalk.cn/course/explore良心课程#xff0c;大家一起来学习哈#xff01;任务22#xff1a;课程介绍1.HTTP 处理过程2.WebHost 的配置与启动3.Middleware 与管道4.Routing MiddleWare 介绍任务23#xff1a;Http请求… 点击蓝字关注我们课程链接http://video.jessetalk.cn/course/explore良心课程大家一起来学习哈任务22课程介绍1.HTTP 处理过程2.WebHost 的配置与启动3.Middleware 与管道4.Routing MiddleWare 介绍任务23Http请求的处理过程任务24WebHost的配置1.覆盖配置文件2.更改启动URL3.IHostingEnvironment4.IApplicationLifetime5.dotnet watch rundotnet new websettings.json{ ConnectionStrings:{ DefaultConnection:Server...;Database...; }}Program.cspublic static IWebHostBuilder CreateWebHostBuilder(string[] args) WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(configureDelegate{ configureDelegate.AddJsonFile(settings.json); }) .UseStartupStartup();Startup.csusing Microsoft.Extensions.Configuration;public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) { // await context.Response.WriteAsync(Hello World!); // JsonFile await context.Response.WriteAsync(configuration[ConnectionStrings:DefaultConnection]); }); }启动HelloCore输出结果Program.cs public static IWebHostBuilder CreateWebHostBuilder(string[] args) WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration(configureDelegate { //configureDelegate.AddJsonFile(settings.json); configureDelegate.AddCommandLine(args); }) //.UseUrls(http://localhost:5001) .UseStartupStartup();Startup.cs app.Run(async (context) { // await context.Response.WriteAsync(Hello World!); // JsonFile //await context.Response.WriteAsync(configuration[ConnectionStrings:DefaultConnection]); // CommandLine await context.Response.WriteAsync($name{configuration[name]}); });设置应用程序参数启动HelloCore输出结果任务25IHostEnvironment和 IApplicationLifetime介绍Startup.csapp.Run(async (context) { await context.Response.WriteAsync($ContentRootPath {env.ContentRootPath}); await context.Response.WriteAsync($EnvironmentName {env.EnvironmentName}); await context.Response.WriteAsync($WebRootPath {env.WebRootPath}); });启动HelloCore输出结果Startup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration, IApplicationLifetime applicationLifetime) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } applicationLifetime.ApplicationStarted.Register(() { Console.WriteLine(Started); }); applicationLifetime.ApplicationStopped.Register(() { Console.WriteLine(Stopped); }); applicationLifetime.ApplicationStopped.Register(() { Console.WriteLine(Stopped); }); app.Run(async (context) { await context.Response.WriteAsync($ContentRootPath {env.ContentRootPath}); await context.Response.WriteAsync($EnvironmentName {env.EnvironmentName}); await context.Response.WriteAsync($WebRootPath {env.WebRootPath}); }); }启动HelloCore输出结果我心中的ASP.NET Core 新核心对象WebHost一http://www.jessetalk.cn/2017/11/11/aspnet-core-object-webhost/#comment-194我心中的ASP.NET Core 新核心对象WebHost二http://www.jessetalk.cn/2017/11/14/aspnet-core-object-webhost-build/任务26dotnet watch run 和attach到进程调试New Terminaldotnet new web --name HelloCoreF5 Start Debug在csproj 的 ItemGroup 添加引用DotNetCliToolReference IncludeMicrosoft.DotNet.Watcher.Tools Version2.0.0 /New Terminaldotnet restoredotnet watch run修改代码保存后会自动重启浏览器刷新即可看到更新结果attach到进程调试任务27Middleware管道介绍1.Middleware 与管道的概念2.用 Middleware 来组成管道实践3.管道的实现机制RequestDelegate 与 ApplicationBuilderstartup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 添加一个中间件传一个名字为next的request delegate app.Use(async (context,next){ await context.Response.WriteAsync(1: before start...); await next.Invoke(); }); // 接收一个RequestDelegate返回一个RequestDelegate app.Use(next{ return (context){ context.Response.WriteAsync(2: in the middle of start..); return next(context); }; }); app.Run(async (context) { await context.Response.WriteAsync(3: start...); }); }启动项目输出结果// 如果不调用next则管道终止不会输出3: start... app.Use(next{ return (context){ return context.Response.WriteAsync(2: in the middle of start..); //return next(context); }; }); // 使用Map构建路由通过localhost:5000/task访问 app.Map(/task, taskApp{ taskApp.Run(async context{ await context.Response.WriteAsync(this is a task); }); }); // 添加一个中间件传一个名字为next的request delegate app.Use(async (context,next){ await context.Response.WriteAsync(1: before start...); await next.Invoke(); }); // 接收一个RequestDelegate返回一个RequestDelegate // 如果不调用next则管道终止不会输出3: start... app.Use(next{ return (context){ context.Response.WriteAsync(2: in the middle of start..); return next(context); }; }); app.Run(async (context) { await context.Response.WriteAsync(3: start...); });访问 https://localhost:5001/task任务28RequestDelegate管道实现思路1.RequestDelegate2.ApplicationBuilder多个RequestDelegate拼接// 添加一个中间件传一个名字为next的RequestDelegateapp.Use(async (context,next){ await context.Response.WriteAsync(1: before start...);// 完成自己处理 await next.Invoke();// 调用下一步});// 封装一个function交给ApplicationBuilder处理app.Use(next{ return (context){ context.Response.WriteAsync(2: in the middle of start..); return next(context); };});任务29自己动手构建RequestDelegate管道新建一个控制台程序dotnet new console --name MyPipeline新建一个类RequestDelegate.csusing System;using System.Threading.Tasks;namespace MyPipeline{ public delegate Task RequestDelegate(Context context);}新建一个类Context.csusing System;using System.Threading.Tasks;namespace MyPipeline{ public class Context { }}using System;using System.Collections.Generic;using System.Threading.Tasks;namespace MyPipeline{ class Program { public static ListFuncRequestDelegate,RequestDelegate _list new ListFuncRequestDelegate, RequestDelegate(); static void Main(string[] args) { Use(next{ return context{ Console.WriteLine(1); return next.Invoke(context); }; }); Use(next{ return context{ Console.WriteLine(2); return next.Invoke(context); }; }); RequestDelegate end (Context){ Console.WriteLine(end...); return Task.CompletedTask; }; _list.Reverse(); foreach(var middleware in _list) { end middleware.Invoke(end); } end.Invoke(new Context()); Console.ReadLine(); } public static void Use(FuncRequestDelegate,RequestDelegate middleware) { _list.Add(middleware); } }}在任何一个Middleware可以结束管道 Use(next{ return context{ Console.WriteLine(1); //return next.Invoke(context); return Task.CompletedTask;// 结束管道调用只输出1 }; });任务30RoutingMiddleware介绍以及MVC引入using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Routing;using Microsoft.Extensions.DependencyInjection;namespace HelloCore{ public class Startup { // 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/?LinkID398940 public void ConfigureServices(IServiceCollection services) { services.AddRouting();// 添加依赖注入配置 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 通过localhost:5000/action访问 app.UseRouter(builderbuilder.MapGet(action, async context{ await context.Response.WriteAsync(this is a action); })); // 使用Map构建路由通过localhost:5000/task访问 app.Map(/task, taskApp{ taskApp.Run(async context{ await context.Response.WriteAsync(this is a task); }); }); // 添加一个中间件传一个名字为next的request delegate app.Use(async (context,next){ await context.Response.WriteAsync(1: before start...); await next.Invoke(); }); // 如果不调用next则管道终止不会输出3: start... app.Use(next{ return (context){ return context.Response.WriteAsync(2: in the middle of start..); //return next(context); }; }); app.Run(async (context) { await context.Response.WriteAsync(3: start...); }); } }}访问 https://localhost:5001/action使用UseRouter方法2using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Routing;using Microsoft.Extensions.DependencyInjection;namespace HelloCore{ public class Startup { // 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/?LinkID398940 public void ConfigureServices(IServiceCollection services) { services.AddRouting();// 添加依赖注入配置 } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用UseRouter方法2 // 通过localhost:5000/action访问 RequestDelegate handler contextcontext.Response.WriteAsync(this is a action); var route new Route( new RouteHandler(handler), action, app.ApplicationServices.GetRequiredServiceIInlineConstraintResolver() ); app.UseRouter(route); // 使用UseRouter方法1 // 通过localhost:5000/action访问 app.UseRouter(builderbuilder.MapGet(action, async context{ await context.Response.WriteAsync(this is a action); })); // 使用Map构建路由通过localhost:5000/task访问 app.Map(/task, taskApp{ taskApp.Run(async context{ await context.Response.WriteAsync(this is a task); }); }); // 添加一个中间件传一个名字为next的request delegate app.Use(async (context,next){ await context.Response.WriteAsync(1: before start...); await next.Invoke(); }); // 如果不调用next则管道终止不会输出3: start... app.Use(next{ return (context){ return context.Response.WriteAsync(2: in the middle of start..); //return next(context); }; }); app.Run(async (context) { await context.Response.WriteAsync(3: start...); }); } }}访问 https://localhost:5001/actionRountingMiddleware介绍var routeHandler new RouteHandler(contextcontext.Response.WriteAsync(test));var route new Route(routeHandler);new RouteMiddleware(route)RouteMiddleware.Invoke(httpContext)_route.RouteAsync(context)routeMatch(RouteContext)OnRouteMatched(RouteContext)点“在看”给我一朵小黄花