From 2b55d92412ee6cdbdffa7921e0289b590eab0d63 Mon Sep 17 00:00:00 2001 From: Anthony Sichi Date: Thu, 20 Feb 2020 09:53:33 -0700 Subject: [PATCH] Migrated SampleWebApp.Core to .NET Core 3.1 LTS. --- SampleWebApp.Core/Controllers/HomeController.cs | 12 ++++++------ SampleWebApp.Core/Program.cs | 14 ++++++++------ SampleWebApp.Core/SampleWebApp.Core.csproj | 10 +++++----- SampleWebApp.Core/Startup.cs | 17 +++++++++++++---- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/SampleWebApp.Core/Controllers/HomeController.cs b/SampleWebApp.Core/Controllers/HomeController.cs index 56b0caf1..b17c456e 100644 --- a/SampleWebApp.Core/Controllers/HomeController.cs +++ b/SampleWebApp.Core/Controllers/HomeController.cs @@ -14,11 +14,11 @@ namespace SampleWebApp.Core.Controllers public class HomeController : Controller { private const string XlsxContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; - private readonly IHostingEnvironment _hostingEnvironment; + private readonly IWebHostEnvironment _webHostEnvironment; - public HomeController(IHostingEnvironment hostingEnvironment) + public HomeController(IWebHostEnvironment webHostEnvironment) { - _hostingEnvironment = hostingEnvironment; + _webHostEnvironment = webHostEnvironment; } /// @@ -31,7 +31,7 @@ public IActionResult FileReport() using (var package = createExcelPackage()) { - package.SaveAs(new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, reportsFolder, fileDownloadName))); + package.SaveAs(new FileInfo(Path.Combine(_webHostEnvironment.WebRootPath, reportsFolder, fileDownloadName))); } return File($"~/{reportsFolder}/{fileDownloadName}", XlsxContentType, fileDownloadName); } @@ -62,7 +62,7 @@ public IActionResult ReadFile() { var fileDownloadName = "report.xlsx"; var reportsFolder = "reports"; - var fileInfo = new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, reportsFolder, fileDownloadName)); + var fileInfo = new FileInfo(Path.Combine(_webHostEnvironment.WebRootPath, reportsFolder, fileDownloadName)); if (!fileInfo.Exists) { using (var package = createExcelPackage()) @@ -207,7 +207,7 @@ private ExcelPackage createExcelPackage() worksheet.Cells[1, 1, 4, 4].AutoFitColumns(); worksheet.HeaderFooter.OddFooter.InsertPicture( - new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, "images", "captcha.jpg")), + new FileInfo(Path.Combine(_webHostEnvironment.WebRootPath, "images", "captcha.jpg")), PictureAlignment.Right); return package; diff --git a/SampleWebApp.Core/Program.cs b/SampleWebApp.Core/Program.cs index 5c2b6674..afceddfd 100644 --- a/SampleWebApp.Core/Program.cs +++ b/SampleWebApp.Core/Program.cs @@ -1,5 +1,6 @@ using System.IO; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace SampleWebApp.Core { @@ -7,14 +8,15 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() - .Build(); + var host = CreateHostBuilder(args).Build(); host.Run(); } + + public static IHostBuilder CreateHostBuilder(string[] args) + { + return Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); + } } } \ No newline at end of file diff --git a/SampleWebApp.Core/SampleWebApp.Core.csproj b/SampleWebApp.Core/SampleWebApp.Core.csproj index 7c7a79a8..92bde1c5 100644 --- a/SampleWebApp.Core/SampleWebApp.Core.csproj +++ b/SampleWebApp.Core/SampleWebApp.Core.csproj @@ -1,13 +1,13 @@  - netcoreapp2.2 + netcoreapp3.1 + + Exe + - - - - + diff --git a/SampleWebApp.Core/Startup.cs b/SampleWebApp.Core/Startup.cs index 1b0d6db5..d656a694 100644 --- a/SampleWebApp.Core/Startup.cs +++ b/SampleWebApp.Core/Startup.cs @@ -8,15 +8,24 @@ public class Startup { public void ConfigureServices(IServiceCollection services) { - services.AddMvc(); + services.AddControllersWithViews(); } - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseDeveloperExceptionPage(); - app.UseMvcWithDefaultRoute(); - app.UseDefaultFiles(); + + app.UseHttpsRedirection(); app.UseStaticFiles(); + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); + }); } } } \ No newline at end of file