ASP.NET Core 的 Program
和 Startup
类演变
本文探讨 Program
和 Startup
类在不同 ASP.NET Core 版本中的演变历程。
ASP.NET Core 6.x
ASP.NET Core 6.x 对 Program
和 Startup
类进行了重大改动:
Program.Main()
模板代码。using
指令。Startup
类,所有代码都位于 Program
文件中。WebApplication
和 WebApplicationBuilder
。ASP.NET Core 6.x 的 Program.cs
文件示例:
<code class="language-csharp">public class Program { public static void Main(string[] args) { var builder = WebApplication.CreateBuilder(args); // 配置服务 builder.Services.AddControllersWithViews(); var app = builder.Build(); // 配置 HTTP 请求管道 app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); } }</code>
ASP.NET Core 3.x 至 5.x
ASP.NET Core 3.x 引入了泛型主机构建器,以支持工作器服务:
<code class="language-csharp">public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }</code>
Startup
类与 2.x 版本类似。
ASP.NET Core 2.x
在 ASP.NET Core 2.x 中,无需在 Startup
构造函数中实例化 IConfiguration
,因为它由 DI 系统注入:
<code class="language-csharp">public Startup(IConfiguration configuration, IHostingEnvironment env) { this.HostingEnvironment = env; this.Configuration = configuration; }</code>
ASP.NET Core 1.x
ASP.NET Core 1.x 需要在 Startup
中显式加载 appsettings
文件:
<code class="language-csharp">public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.Configuration = builder.Build(); }</code>
从 appsettings.json
获取值
主要有两种方法可以从 appsettings.json
获取值:
简单方法
通过 IConfiguration
将整个配置注入到控制器或类中,并使用指定的键获取所需的值:
<code class="language-csharp">public class AccountController : Controller { private readonly IConfiguration _config; public AccountController(IConfiguration config) { _config = config; } public IActionResult ResetPassword() { var vm = new ResetPasswordViewModel { PasswordRequiredLength = _config.GetValue<int>("AppIdentitySettings:Password:RequiredLength"), RequireUppercase = _config.GetValue<bool>("AppIdentitySettings:Password:RequireUppercase") }; return View(vm); } }</code>
选项模式 (Options Pattern)
对于多个或复杂的设置,选项模式使用类来表示配置的层次结构:
<code class="language-csharp">public class AppIdentitySettings { public UserSettings User { get; set; } public PasswordSettings Password { get; set; } public LockoutSettings Lockout { get; set; } } // ... (UserSettings, PasswordSettings, LockoutSettings classes as before) ...</code>
Startup
的 ConfigureServices
中注册配置实例:<code class="language-csharp">var identitySettingsSection = _configuration.GetSection("AppIdentitySettings"); services.Configure<AppIdentitySettings>(identitySettingsSection);</code>
IOptions<AppIdentitySettings>
注入到使用它的控制器/类中:<code class="language-csharp">public class AccountController : Controller { private readonly AppIdentitySettings _appIdentitySettings; public AccountController(IOptions<AppIdentitySettings> appIdentitySettingsAccessor) { _appIdentitySettings = appIdentitySettingsAccessor.Value; } public IActionResult ResetPassword() { var vm = new ResetPasswordViewModel { PasswordRequiredLength = _appIdentitySettings.Password.RequiredLength, RequireUppercase = _appIdentitySettings.Password.RequireUppercase }; return View(vm); } }</code>
以上是ASP.NET Core 的程序和启动类在不同版本中是如何演变的?的详细内容。更多信息请关注PHP中文网其他相关文章!