From 27623a4e93ba8667dfd419e31e1add61f29004d2 Mon Sep 17 00:00:00 2001 From: cl Date: Wed, 21 Oct 2026 14:57:52 +0800 Subject: [PATCH] fix: do not publish `manifest.json` to static files folder. --- .../Controllers/ApiController.filter.cs | 2 +- .../Controllers/DefaultController.cs | 2 +- WebApplication1/ViteHelper.cs | 21 +++++++------------ WebApplication1/WebApplication1.csproj | 9 +++++--- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/WebApplication1/Controllers/ApiController.filter.cs b/WebApplication1/Controllers/ApiController.filter.cs index 379971b..44487ba 100644 --- a/WebApplication1/Controllers/ApiController.filter.cs +++ b/WebApplication1/Controllers/ApiController.filter.cs @@ -109,7 +109,7 @@ class PageExceptionFilterAttribute : ExceptionFilterAttribute var helper = context.HttpContext.RequestServices.GetRequiredService(); var html = await helper.RenderAsync( context.HttpContext.Request.PathBase, - "Templates/error.html", + "error.html", "src/pages/error.ts", new ApiErrorResult(errorCode, errorMessage), new() { ["Title"] = "Error Page" }); diff --git a/WebApplication1/Controllers/DefaultController.cs b/WebApplication1/Controllers/DefaultController.cs index 8940c32..4e5d3fa 100644 --- a/WebApplication1/Controllers/DefaultController.cs +++ b/WebApplication1/Controllers/DefaultController.cs @@ -15,7 +15,7 @@ public class DefaultController(ViteHelper vite) : SiteControllerBase(vite) var html = await viteHelper.RenderAsync( Request.PathBase, - "Templates/main.html", + "main.html", "src/main.ts", new() { ["Title"] = "Home Page" }); diff --git a/WebApplication1/ViteHelper.cs b/WebApplication1/ViteHelper.cs index d42bfb6..8e66ab4 100644 --- a/WebApplication1/ViteHelper.cs +++ b/WebApplication1/ViteHelper.cs @@ -4,29 +4,24 @@ using System.Text.Json; namespace WebApplication1; -#if DEBUG public class ViteHelper(IOptions options) { -#else -public class ViteHelper(IWebHostEnvironment env, IOptions options) -{ - readonly string rootPath = env.WebRootPath ?? Path.Combine(AppContext.BaseDirectory, "wwwroot"); -#endif readonly JsonSerializerOptions jsonOptions = options.Value.JsonSerializerOptions; - public Task RenderAsync(string pathBase, string templatePath, string entryScript, Dictionary stringTokens = null) + public Task RenderAsync(string pathBase, string template, string entryScript, Dictionary stringTokens = null) { - return RenderInternalAsync(pathBase, templatePath, entryScript, null, stringTokens); + return RenderInternalAsync(pathBase, template, entryScript, null, stringTokens); } - public Task RenderAsync(string pathBase, string templatePath, string entryScript, T serverData = default, Dictionary stringTokens = null) + public Task RenderAsync(string pathBase, string template, string entryScript, T serverData = default, Dictionary stringTokens = null) { - return RenderInternalAsync(pathBase, templatePath, entryScript, serverData, stringTokens); + return RenderInternalAsync(pathBase, template, entryScript, serverData, stringTokens); } - async Task RenderInternalAsync(string pathBase, string templatePath, string entryScript, object serverData = null, Dictionary stringTokens = null) + async Task RenderInternalAsync(string pathBase, string template, string entryScript, object serverData = null, Dictionary stringTokens = null) { - var path = Path.Combine(AppContext.BaseDirectory, templatePath); + var templateFolder = Path.Combine(AppContext.BaseDirectory, "Templates"); + var path = Path.Combine(templateFolder, template); var html = await File.ReadAllTextAsync(path); if (stringTokens is not null) @@ -47,7 +42,7 @@ public class ViteHelper(IWebHostEnvironment env, IOptions options) """; #else - var manifestPath = Path.Combine(rootPath, "dist", ".vite", "manifest.json"); + var manifestPath = Path.Combine(templateFolder, "manifest.json"); var manifestJson = await File.ReadAllTextAsync(manifestPath); using var manifest = JsonDocument.Parse(manifestJson); diff --git a/WebApplication1/WebApplication1.csproj b/WebApplication1/WebApplication1.csproj index e204d5d..b696a7b 100644 --- a/WebApplication1/WebApplication1.csproj +++ b/WebApplication1/WebApplication1.csproj @@ -16,9 +16,6 @@ PreserveNewest - - PreserveNewest - @@ -32,6 +29,12 @@ + + + Templates\manifest.json + Always + +