From 97d92a8f3c028225bd12bed597f80f563b162c42 Mon Sep 17 00:00:00 2001 From: Jeffrey <572199+darkthread@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:41:45 +0800 Subject: [PATCH] Enable .csthml views --- Controllers/CryptoController.cs | 2 +- Program.cs | 2 +- Views/Crypto/Sha1.cshtml | 2 ++ Views/Shared/Error.cshtml | 14 ++++++++++++++ Views/Shared/_Layout.cshtml | 13 +++++++++++++ Views/_ViewStart.cshtml | 3 +++ min-api-add-mvc.csproj | 5 +++++ wwwroot/index.html | 2 +- 8 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Views/Crypto/Sha1.cshtml create mode 100644 Views/Shared/Error.cshtml create mode 100644 Views/Shared/_Layout.cshtml create mode 100644 Views/_ViewStart.cshtml diff --git a/Controllers/CryptoController.cs b/Controllers/CryptoController.cs index 1468f6f..caaf05c 100644 --- a/Controllers/CryptoController.cs +++ b/Controllers/CryptoController.cs @@ -13,7 +13,7 @@ public IActionResult Sha1(string data) SHA1.Create().ComputeHash( Encoding.UTF8.GetBytes(data.ToString()))) .Replace("-", string.Empty); - return Content($"
{hash}
", "text/html"); + return View("Sha1", hash); } } } diff --git a/Program.cs b/Program.cs index 4c5dc24..353a917 100644 --- a/Program.cs +++ b/Program.cs @@ -4,7 +4,7 @@ var builder = WebApplication.CreateBuilder(args); // 啟用 Controller -builder.Services.AddControllers(); +builder.Services.AddMvc(); var app = builder.Build(); diff --git a/Views/Crypto/Sha1.cshtml b/Views/Crypto/Sha1.cshtml new file mode 100644 index 0000000..2e9aa32 --- /dev/null +++ b/Views/Crypto/Sha1.cshtml @@ -0,0 +1,2 @@ +@model string +
@Model
diff --git a/Views/Shared/Error.cshtml b/Views/Shared/Error.cshtml new file mode 100644 index 0000000..e514139 --- /dev/null +++ b/Views/Shared/Error.cshtml @@ -0,0 +1,14 @@ +@{ + ViewData["Title"] = "Error"; +} + +

Error.

+

An error occurred while processing your request.

+ +

Development Mode

+

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

+

+ Development environment should not be enabled in deployed applications, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the ASPNETCORE_ENVIRONMENT environment variable to Development, and restarting the application. +

diff --git a/Views/Shared/_Layout.cshtml b/Views/Shared/_Layout.cshtml new file mode 100644 index 0000000..3c25d93 --- /dev/null +++ b/Views/Shared/_Layout.cshtml @@ -0,0 +1,13 @@ + + + + + + @ViewBag.Title + + +
+ @RenderBody() +
+ + diff --git a/Views/_ViewStart.cshtml b/Views/_ViewStart.cshtml new file mode 100644 index 0000000..a5f1004 --- /dev/null +++ b/Views/_ViewStart.cshtml @@ -0,0 +1,3 @@ +@{ + Layout = "_Layout"; +} diff --git a/min-api-add-mvc.csproj b/min-api-add-mvc.csproj index be2c54a..40e4ef1 100644 --- a/min-api-add-mvc.csproj +++ b/min-api-add-mvc.csproj @@ -7,4 +7,9 @@ min_api_add_mvc + + + + + diff --git a/wwwroot/index.html b/wwwroot/index.html index 22fcb21..bfaadc7 100644 --- a/wwwroot/index.html +++ b/wwwroot/index.html @@ -6,7 +6,7 @@