From dbdd8a91899c50dd3389dfc35986a7744b461e8a Mon Sep 17 00:00:00 2001 From: Marcello Lamonaca Date: Thu, 4 Feb 2021 19:26:20 +0100 Subject: [PATCH] Move ASP.NET Core config notes to dedicated file --- .NET/ASP.NET/Configuration.md | 163 ++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 .NET/ASP.NET/Configuration.md diff --git a/.NET/ASP.NET/Configuration.md b/.NET/ASP.NET/Configuration.md new file mode 100644 index 0000000..f38c75d --- /dev/null +++ b/.NET/ASP.NET/Configuration.md @@ -0,0 +1,163 @@ +# ASP.NET Configuration + +## `Program.cs` + +```cs +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; + +namespace App +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); // start and config ASP.NET App + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); // config handled in Startup.cs + }); + } +} +``` + +## `Startup.cs` + +```cs +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace App +{ + 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 DI container. + public void ConfigureServices(IServiceCollection services) + { + // set db context for the app using the connection string + services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + + // Captures synchronous and asynchronous Exception instances from the pipeline and generates HTML error responses. + services.AddDatabaseDeveloperPageExceptionFilter(); + + // use Razor Pages, runtime compilation needs Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation pkg + services.AddRazorPages().AddRazorRuntimeCompilation(); + // or + services.AddControllers(); // controllers w/o views + //or + sevices.AddControllersWithViews(); // MVC Controllers + + // set dependency injection lifetimes + services.AddSingleton(); + services.AddScoped(); + services.AddTransient(); + + } + + // 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("/Home/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 => + { + // MVC routing + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}" + ); + // or + endpoints.MapControllers(); // map controllers w/o views + // or + endpoints.MapRazorPages(); + }); + } + } +} +``` + +## `appsettings.json` + +Connection Strings & Secrets. + +```json +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} +``` + +## `launchsettings.json` + +Launch Settings & Deployement Settings. + +```json +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51144", + "sslPort": 44335 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "BookListRazor": { + "commandName": "Project", + "dotnetRunMessages": "true", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} +```