Skip to content

Commit 8e130af

Browse files
committed
refactor: 更新根目录变量
1 parent 7fab213 commit 8e130af

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

src/BootstrapBlazor.Server/Controllers/WaterfallController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public async Task<IActionResult> Index([FromServices] IOptions<WebsiteOptions> o
2424
var interval = Random.Shared.Next(1, 2000);
2525
await Task.Delay(interval);
2626

27-
var fileName = Path.Combine(options.Value.ContentRootPath, "../BootstrapBlazor.Shared/wwwroot/images/waterfall", $"{id}.jpeg");
27+
#if DEBUG
28+
var fileName = Path.Combine(options.Value.WebRootPath, "../../BootstrapBlazor.Shared/wwwroot/images/waterfall", $"{id}.jpeg");
29+
#else
30+
var fileName = Path.Combine(options.Value.WebRootPath, "_content/BootstrapBlazor.Shared/wwwroot/images/waterfall", $"{id}.jpeg");
31+
#endif
2832
return new PhysicalFileResult(fileName, "images/jpeg");
2933
}
3034
}

src/BootstrapBlazor.Server/Services/ClearUploadFilesService.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,14 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Longbow.Tasks;
7-
using Microsoft.Extensions.Options;
87

98
namespace BootstrapBlazor.Service.Services;
109

1110
/// <summary>
1211
/// 后台任务服务类
1312
/// </summary>
14-
internal class ClearTempFilesService : BackgroundService
13+
internal class ClearTempFilesService(IWebHostEnvironment env) : BackgroundService
1514
{
16-
private readonly IWebHostEnvironment _env;
17-
18-
/// <summary>
19-
///
20-
/// </summary>
21-
/// <param name="env"></param>
22-
/// <param name="websiteOption"></param>
23-
public ClearTempFilesService(IWebHostEnvironment env, IOptionsMonitor<WebsiteOptions> websiteOption)
24-
{
25-
_env = env;
26-
websiteOption.CurrentValue.WebRootPath = env.WebRootPath;
27-
websiteOption.CurrentValue.ContentRootPath = env.ContentRootPath;
28-
websiteOption.CurrentValue.IsDevelopment = env.IsDevelopment();
29-
}
30-
3115
/// <summary>
3216
/// 运行任务
3317
/// </summary>
@@ -37,14 +21,14 @@ protected override Task ExecuteAsync(CancellationToken stoppingToken)
3721
{
3822
TaskServicesManager.GetOrAdd("Clear Upload Files", (provider, token) =>
3923
{
40-
var filePath = Path.Combine(_env.WebRootPath, "images", "uploader");
24+
var filePath = Path.Combine(env.WebRootPath, "images", "uploader");
4125
if (Directory.Exists(filePath))
4226
{
4327
Directory.EnumerateFiles(filePath).Take(10).ToList().ForEach(file => DeleteFile(file, token));
4428
}
4529

4630
// 清除导出临时文件
47-
var exportFilePath = Path.Combine(_env.WebRootPath, "pdf");
31+
var exportFilePath = Path.Combine(env.WebRootPath, "pdf");
4832
if (Directory.Exists(exportFilePath))
4933
{
5034
Directory.EnumerateFiles(exportFilePath, "*.html").Take(10).Where(file =>

src/BootstrapBlazor.Shared/Data/WebsiteOptions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,11 @@ public WebsiteOptions()
150150
Videos = config.GetSection("video").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToDictionary(item => item.Key, item => item.Value);
151151
Links = config.GetSection("link").GetChildren().Select(c => new KeyValuePair<string, string?>(c.Key, c.Value)).ToDictionary(item => item.Key, item => item.Value);
152152

153-
AssetRootPath = "./_content/BootstrapBlazor.Shared/";
154-
ContentRootPath = Path.Combine(AppContext.BaseDirectory, "../../../");
155-
WebRootPath = Path.Combine(AppContext.BaseDirectory, "../../wwwroot/");
153+
#if DEBUG
154+
IsDevelopment = true;
155+
#endif
156+
ContentRootPath = IsDevelopment ? Path.Combine(AppContext.BaseDirectory, "../../../") : AppContext.BaseDirectory;
157+
WebRootPath = Path.Combine(ContentRootPath, "wwwroot");
156158
}
157159

158160
private IConfiguration GetConfiguration(string jsonFileName)

0 commit comments

Comments
 (0)