From 59488bb4132444b579174576500a16b6c1b55159 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Thu, 14 Sep 2023 21:00:44 +0100 Subject: [PATCH] chore(#1): Restructure project as mvc --- WhoAtX/Controllers/HomeController.cs | 31 +++++++++++++++++++ WhoAtX/Models/ErrorViewModel.cs | 8 +++++ WhoAtX/Pages/Error.cshtml.cs | 27 ---------------- WhoAtX/Pages/Index.cshtml.cs | 19 ------------ WhoAtX/Pages/Privacy.cshtml.cs | 19 ------------ WhoAtX/Program.cs | 8 +++-- WhoAtX/Properties/launchSettings.json | 10 +++--- WhoAtX/{Pages => Views/Home}/Index.cshtml | 6 ++-- WhoAtX/{Pages => Views/Home}/Privacy.cshtml | 4 +-- WhoAtX/{Pages => Views/Shared}/Error.cshtml | 5 ++- WhoAtX/{Pages => Views}/Shared/_Layout.cshtml | 14 ++++----- .../Shared/_Layout.cshtml.css | 0 .../Shared/_ValidationScriptsPartial.cshtml | 0 WhoAtX/{Pages => Views}/_ViewImports.cshtml | 2 +- WhoAtX/{Pages => Views}/_ViewStart.cshtml | 0 WhoAtX/appsettings.Development.json | 1 - 16 files changed, 61 insertions(+), 93 deletions(-) create mode 100644 WhoAtX/Controllers/HomeController.cs create mode 100644 WhoAtX/Models/ErrorViewModel.cs delete mode 100644 WhoAtX/Pages/Error.cshtml.cs delete mode 100644 WhoAtX/Pages/Index.cshtml.cs delete mode 100644 WhoAtX/Pages/Privacy.cshtml.cs rename WhoAtX/{Pages => Views/Home}/Index.cshtml (70%) rename WhoAtX/{Pages => Views/Home}/Privacy.cshtml (76%) rename WhoAtX/{Pages => Views/Shared}/Error.cshtml (79%) rename WhoAtX/{Pages => Views}/Shared/_Layout.cshtml (82%) rename WhoAtX/{Pages => Views}/Shared/_Layout.cshtml.css (100%) rename WhoAtX/{Pages => Views}/Shared/_ValidationScriptsPartial.cshtml (100%) rename WhoAtX/{Pages => Views}/_ViewImports.cshtml (72%) rename WhoAtX/{Pages => Views}/_ViewStart.cshtml (100%) diff --git a/WhoAtX/Controllers/HomeController.cs b/WhoAtX/Controllers/HomeController.cs new file mode 100644 index 0000000..953bd9e --- /dev/null +++ b/WhoAtX/Controllers/HomeController.cs @@ -0,0 +1,31 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using WhoAtX.Models; + +namespace WhoAtX.Controllers; + +public class HomeController : Controller +{ + private readonly ILogger _logger; + + public HomeController(ILogger logger) + { + _logger = logger; + } + + public IActionResult Index() + { + return View(); + } + + public IActionResult Privacy() + { + return View(); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } +} diff --git a/WhoAtX/Models/ErrorViewModel.cs b/WhoAtX/Models/ErrorViewModel.cs new file mode 100644 index 0000000..729bc90 --- /dev/null +++ b/WhoAtX/Models/ErrorViewModel.cs @@ -0,0 +1,8 @@ +namespace WhoAtX.Models; + +public class ErrorViewModel +{ + public string? RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); +} diff --git a/WhoAtX/Pages/Error.cshtml.cs b/WhoAtX/Pages/Error.cshtml.cs deleted file mode 100644 index 4d53570..0000000 --- a/WhoAtX/Pages/Error.cshtml.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Diagnostics; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace WhoAtX.Pages; - -[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] -[IgnoreAntiforgeryToken] -public class ErrorModel : PageModel -{ - public string? RequestId { get; set; } - - public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - - private readonly ILogger _logger; - - public ErrorModel(ILogger logger) - { - _logger = logger; - } - - public void OnGet() - { - RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; - } -} - diff --git a/WhoAtX/Pages/Index.cshtml.cs b/WhoAtX/Pages/Index.cshtml.cs deleted file mode 100644 index aaa024f..0000000 --- a/WhoAtX/Pages/Index.cshtml.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace WhoAtX.Pages; - -public class IndexModel : PageModel -{ - private readonly ILogger _logger; - - public IndexModel(ILogger logger) - { - _logger = logger; - } - - public void OnGet() - { - - } -} diff --git a/WhoAtX/Pages/Privacy.cshtml.cs b/WhoAtX/Pages/Privacy.cshtml.cs deleted file mode 100644 index 96419ca..0000000 --- a/WhoAtX/Pages/Privacy.cshtml.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace WhoAtX.Pages; - -public class PrivacyModel : PageModel -{ - private readonly ILogger _logger; - - public PrivacyModel(ILogger logger) - { - _logger = logger; - } - - public void OnGet() - { - } -} - diff --git a/WhoAtX/Program.cs b/WhoAtX/Program.cs index b580f44..4ac679a 100644 --- a/WhoAtX/Program.cs +++ b/WhoAtX/Program.cs @@ -1,14 +1,14 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. -builder.Services.AddRazorPages(); +builder.Services.AddControllersWithViews(); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { - app.UseExceptionHandler("/Error"); + 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(); } @@ -20,6 +20,8 @@ app.UseRouting(); app.UseAuthorization(); -app.MapRazorPages(); +app.MapControllerRoute( + name: "default", + pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run(); diff --git a/WhoAtX/Properties/launchSettings.json b/WhoAtX/Properties/launchSettings.json index e305884..fcedae5 100644 --- a/WhoAtX/Properties/launchSettings.json +++ b/WhoAtX/Properties/launchSettings.json @@ -1,10 +1,10 @@ -{ +{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:63923", - "sslPort": 44396 + "applicationUrl": "http://localhost:52536", + "sslPort": 44370 } }, "profiles": { @@ -12,7 +12,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5262", + "applicationUrl": "http://localhost:5030", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -21,7 +21,7 @@ "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "https://localhost:7148;http://localhost:5262", + "applicationUrl": "https://localhost:7161;http://localhost:5030", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/WhoAtX/Pages/Index.cshtml b/WhoAtX/Views/Home/Index.cshtml similarity index 70% rename from WhoAtX/Pages/Index.cshtml rename to WhoAtX/Views/Home/Index.cshtml index 8a5f8e3..08a5a5c 100644 --- a/WhoAtX/Pages/Index.cshtml +++ b/WhoAtX/Views/Home/Index.cshtml @@ -1,7 +1,5 @@ -@page -@model IndexModel -@{ - ViewData["Title"] = "Home page"; +@{ + ViewData["Title"] = "Home Page"; }
diff --git a/WhoAtX/Pages/Privacy.cshtml b/WhoAtX/Views/Home/Privacy.cshtml similarity index 76% rename from WhoAtX/Pages/Privacy.cshtml rename to WhoAtX/Views/Home/Privacy.cshtml index 5c16860..2479fb7 100644 --- a/WhoAtX/Pages/Privacy.cshtml +++ b/WhoAtX/Views/Home/Privacy.cshtml @@ -1,6 +1,4 @@ -@page -@model PrivacyModel -@{ +@{ ViewData["Title"] = "Privacy Policy"; }

@ViewData["Title"]

diff --git a/WhoAtX/Pages/Error.cshtml b/WhoAtX/Views/Shared/Error.cshtml similarity index 79% rename from WhoAtX/Pages/Error.cshtml rename to WhoAtX/Views/Shared/Error.cshtml index 09da0d2..4fa9d25 100644 --- a/WhoAtX/Pages/Error.cshtml +++ b/WhoAtX/Views/Shared/Error.cshtml @@ -1,5 +1,4 @@ -@page -@model ErrorModel +@model ErrorViewModel @{ ViewData["Title"] = "Error"; } @@ -16,7 +15,7 @@

Development Mode

- Swapping to the Development environment displays detailed information about the error that occurred. + Swapping to Development environment will display more detailed information about the error that occurred.

The Development environment shouldn't be enabled for deployed applications. diff --git a/WhoAtX/Pages/Shared/_Layout.cshtml b/WhoAtX/Views/Shared/_Layout.cshtml similarity index 82% rename from WhoAtX/Pages/Shared/_Layout.cshtml rename to WhoAtX/Views/Shared/_Layout.cshtml index 2f18bfe..2ddff30 100644 --- a/WhoAtX/Pages/Shared/_Layout.cshtml +++ b/WhoAtX/Views/Shared/_Layout.cshtml @@ -11,8 +11,8 @@