- public static IwebHostBuilder CreateDefaultBuilder(string[] args)
- {
- var builder = new WebHostBuilder();
- if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
- {
- builder.UseContentRoot(Directory.GetCurrentDirectory());
- }
- if (args != null)
- {
- builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
- }
- builder.UseKestrel((builderContext, options) =>
- {
- options.Configure(builderContext.Configuration.GetSection("Kestrel"));
- })
- .ConfigureAppConfiguration((hostingContext, config) =>
- {
- var env = hostingContext.HostingEnvironment;
- config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
- .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
- if (env.IsDevelopment())
- {
- var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
- if (appAssembly != null)
- {
- config.AddUserSecrets(appAssembly, optional: true);
- }
- }
- config.AddEnvironmentVariables();
- if (args != null)
- {
- config.AddCommandLine(args);
- }
- })
- .ConfigureLogging((hostingContext, logging) =>
- {
- logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
- logging.AddConsole();
- logging.AddDebug();
- })
- .ConfigureServices((hostingContext, services) =>
- {
- // Fallback
- services.PostConfigure<HostFilteringOptions>(options =>
- {
- if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
- {
- // "AllowedHosts": "localhost;127.0.0.1;[::1]"
- var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- // Fall back to "*" to disable.
- options.AllowedHosts = (hosts?.Length> 0 ? hosts : new[] { "*" });
- }
- });
- // Change notification
- services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
- new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));
- services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
- })
- .UseIISIntegration()
- .UseDefaultServiceProvider((context, options) =>
- {
- options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
- });
- return builder;
- }
来源: https://www.cnblogs.com/wtzbk/p/9196185.html