From 08f14d8ae9f6374ab2d275d3d4df42c33959c260 Mon Sep 17 00:00:00 2001 From: Will Date: Fri, 1 Sep 2023 12:48:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A4=BA=E7=AF=84=20HtmlEncoder=20=E8=88=87=20?= =?UTF-8?q?JavaScriptEncoder=20=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/HomeController.cs | 2 ++ Program.cs | 7 +++++++ Views/Home/Index.cshtml | 8 ++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index c7128a8..9617193 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -15,6 +15,8 @@ public HomeController(ILogger logger) public IActionResult Index() { + ViewBag.Message = "如何用 'ASP.NET Core' 打造 Web 應用程式"; + return View(); } diff --git a/Program.cs b/Program.cs index 0727468..6b18fd6 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,15 @@ +using System.Text.Encodings.Web; +using System.Text.Unicode; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddSingleton( + HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin, + UnicodeRanges.CjkUnifiedIdeographs })); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index d2d19bd..98a13c0 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -1,8 +1,12 @@ @{ - ViewData["Title"] = "Home Page"; + var jsEncoder = System.Text.Encodings.Web.JavaScriptEncoder.Default; } + +

Welcome

-

Learn about building Web apps with ASP.NET Core.

+

@ViewBag.Message