⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制”
1 { 2 "Logging": { 3 "LogLevel": { 4 "Default": "Warning" 5 } 6 }, 7 "AllowedHosts": "*" 8 }
1 { 2 "ConnectionStrings": { 3 "StudyConnStr": "Data Source=.;Initial Catalog=Study;User ID=sa;Password=admin" 4 } 5 }
⒉在Program中加载配置文件
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Threading.Tasks; 6 using Microsoft.AspNetCore; 7 using Microsoft.AspNetCore.Hosting; 8 using Microsoft.Extensions.Configuration; 9 using Microsoft.Extensions.Logging; 10 11 namespace EF_SqlServer 12 { 13 public class Program 14 { 15 public static void Main(string[] args) 16 { 17 CreateWebHostBuilder(args).Build().Run(); 18 19 } 20 21 public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 22 WebHost.CreateDefaultBuilder(args) 23 .ConfigureAppConfiguration((hostingContext, config) => 24 { 25 config.SetBasePath(Directory.GetCurrentDirectory()); 26 config.AddJsonFile("//Config//dbconfig.json", true, true); 27 config.AddJsonFile("appsettings.json", true, true); 28 }).UseStartup<Startup>(); 29 } 30 }
⒊使用配置文件中的相关属性
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Builder; 6 using Microsoft.AspNetCore.Hosting; 7 using Microsoft.AspNetCore.Http; 8 using Microsoft.AspNetCore.HttpsPolicy; 9 using Microsoft.AspNetCore.Mvc; 10 using Microsoft.Extensions.Configuration; 11 using Microsoft.Extensions.DependencyInjection; 12 13 namespace EF_SqlServer 14 { 15 public class Startup 16 { 17 public Startup(IConfiguration configuration) 18 { 19 Configuration = configuration; 20 } 21 22 public IConfiguration Configuration { get; } 23 24 // This method gets called by the runtime. Use this method to add services to the container. 25 public void ConfigureServices(IServiceCollection services) 26 { 27 services.Configure<CookiePolicyOptions>(options => 28 { 29 // This lambda determines whether user consent for non-essential cookies is needed for a given request. 30 options.CheckConsentNeeded = context => true; 31 options.MinimumSameSitePolicy = SameSiteMode.None; 32 }); 33 string dbConn = Configuration.GetSection("ConnectionStrings").GetSection("StudyConnStr").Value; 34 string logDef = Configuration["Logging:LogLevel:Default"]; 35 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); 36 } 37 38 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 39 public void Configure(IApplicationBuilder app, IHostingEnvironment env) 40 { 41 if (env.IsDevelopment()) 42 { 43 app.UseDeveloperExceptionPage(); 44 } 45 else 46 { 47 app.UseExceptionHandler("/Home/Error"); 48 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 49 app.UseHsts(); 50 } 51 52 app.UseHttpsRedirection(); 53 app.UseStaticFiles(); 54 app.UseCookiePolicy(); 55 56 app.UseMvc(routes => 57 { 58 routes.MapRoute( 59 name: "default", 60 template: "{controller=Home}/{action=Index}/{id?}"); 61 }); 62 } 63 } 64 }
原文地址:https://www.cnblogs.com/fanqisoft/p/10806862.html
时间: 2024-11-02 15:43:12