时间:2023-04-21 11:00:02 | 来源:网站运营
时间:2023-04-21 11:00:02 来源:网站运营
[Asp.Net Core] 系列教程 (二):using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Http;using Microsoft.Extensions.DependencyInjection;using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace One{ 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/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // 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(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }}
Startup 类可以用来定义HTTP请求处理管道和配置应用程序需要的服务。//定义应用程序所需要的服务,例如 ASP.NET Core MVC、Entity Framework Core 和 Identity 等public void ConfigureServices(IServiceCollection services){}
//定义请求管道中的中间件,该方法可以用来定义我们的应用程序如何响应请求public void Configure(IApplicationBuilder app, IHostingEnvironment env){}
如果我们希望应用程序的有不同的行为,我们就需要在 Configure() 方法中添加其他代码来更改管道。app.Run(async (context) =>{ await context.Response.WriteAsync("Hello World!");});
我们也可以更改其内容,例如:app.Run(async (context) =>{ await context.Response.WriteAsync("Hello EggMo!");});
那么应用启动后显示的响应就是:关键词:教程,系列