[ASP.NET Core开发实战]基础篇01 Startup
2021-01-22 04:12
标签:定向 fir ram 自定义服务 for containe cal change rtu Startup,顾名思义,就是启动类,用于配置ASP.NET Core应用的服务和请求管道。 Startup有两个主要作用: 下面是VS通过ASP.NET Core项目模板生成的Startup类。 ConfigureServices方法: IServiceCollection上有很多服务扩展方法,以Add{Service}格式命名,例如AddDbContext、AddDefaultIdentity;因此,约定俗成,在自定义服务时,也要以Add{Service}定义扩展方法。 Configure方法用于指定应用响应HTTP请求的方式。可通过将中间件组件添加到IApplicationBuilder实例来配置请求管道。 ASP.NET Core内置配置的管道包括: 每个Use扩展方法将一个或多个中间件组件添加到请求管道。因此,自定义中间件组件也要按Use来定义扩展方法。 请求管道中的每个中间件组件负责调用管道中的下一个组件,或在适当情况下使链发生短路。因此注册中间件时要注意调用顺序,不然会有意想不到的情况出现。 有关如何使用IApplicationBuilder和中间件处理顺序的详细信息,查看ASP.NET Core中间件。 不使用Startup类配置服务,那就需要在创建主机实例时调用ConfigureServices和Configure方法。如下: [ASP.NET Core开发实战]基础篇01 Startup 标签:定向 fir ram 自定义服务 for containe cal change rtu 原文地址:https://www.cnblogs.com/liang24/p/13289217.html
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.AddRazorPages();
}
// 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();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
ConfigureServices方法
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity
Configure方法
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
不使用Startup的情况下配置服务
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices(services =>
{
services.AddControllersWithViews();
})
.Configure(app =>
{
var loggerFactory = app.ApplicationServices
.GetRequiredService
文章标题:[ASP.NET Core开发实战]基础篇01 Startup
文章链接:http://soscw.com/index.php/essay/45273.html