前言
Volo.Abp.VirtualFileSystem
是ABP(ASP.NET Boilerplate)框架中的一個重要元件,它提供了一種抽象檔案系統的方式,使得應用程式可以輕鬆地訪問和管理檔案資源,無論這些資源是來自於物理檔案系統、嵌入資源,還是遠端儲存。
透過Volo.Abp.VirtualFileSystem
,開發者可以使用統一的介面來處理檔案和目錄,而不用關心這些檔案和目錄的實際儲存位置。這使得應用程式更加靈活,可以輕鬆地切換不同的檔案儲存方式,而不用修改大量的程式碼。
新建mvc專案
引用以下nuget包
新建BookAppWebModule.cs
using Volo.Abp.Localization.ExceptionHandling;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
using Volo.Abp.Autofac;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Hosting.Internal;
using BookApp.Localization;
using BookCategory;
namespace BookApp
{
[DependsOn(
typeof(AbpAutofacModule),
typeof(AbpLocalizationModule),
typeof(AbpVirtualFileSystemModule),
typeof(AbpAspNetCoreMvcModule),
typeof(BookCategoryModule)
)]
public class BookAppWebModule: AbpModule
{
public override void PreConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
context.Services.PreConfigure<AbpMvcDataAnnotationsLocalizationOptions>(options =>
{
options.AddAssemblyResource(
typeof(BookStoreResource)
);
});
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
ConfigureVirtualFileSystem(hostingEnvironment);
Configure<AbpLocalizationOptions>(options =>
{
options.Languages.Add(new LanguageInfo("ar", "ar", "العربية"));
options.Languages.Add(new LanguageInfo("cs", "cs", "Čeština"));
options.Languages.Add(new LanguageInfo("en", "en", "English"));
options.Languages.Add(new LanguageInfo("en-GB", "en-GB", "English (UK)"));
options.Languages.Add(new LanguageInfo("hu", "hu", "Magyar"));
options.Languages.Add(new LanguageInfo("fi", "fi", "Finnish"));
options.Languages.Add(new LanguageInfo("fr", "fr", "Français"));
options.Languages.Add(new LanguageInfo("hi", "hi", "Hindi"));
options.Languages.Add(new LanguageInfo("it", "it", "Italiano"));
options.Languages.Add(new LanguageInfo("pt-BR", "pt-BR", "Português"));
options.Languages.Add(new LanguageInfo("ru", "ru", "Русский"));
options.Languages.Add(new LanguageInfo("sk", "sk", "Slovak"));
options.Languages.Add(new LanguageInfo("tr", "tr", "Türkçe"));
options.Languages.Add(new LanguageInfo("zh-Hans", "zh-Hans", "簡體中文"));
options.Languages.Add(new LanguageInfo("zh-Hant", "zh-Hant", "繁體中文"));
options.Languages.Add(new LanguageInfo("de-DE", "de-DE", "Deutsch"));
options.Languages.Add(new LanguageInfo("es", "es", "Español"));
options.Resources
.Add<BookStoreResource>("en")
.AddVirtualJson("/Localization/BookStore");
options.DefaultResourceType = typeof(BookStoreResource);
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var app = context.GetApplicationBuilder();
var env = context.GetEnvironment();
app.UseAbpRequestLocalization();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
}
private void ConfigureVirtualFileSystem(IWebHostEnvironment hostingEnvironment)
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<BookAppWebModule>();
if (hostingEnvironment.IsDevelopment())
{
options.FileSets.ReplaceEmbeddedByPhysical<BookAppWebModule>(hostingEnvironment.ContentRootPath);
options.FileSets.ReplaceEmbeddedByPhysical<BookCategoryModule>(Path.Combine(hostingEnvironment.ContentRootPath, string.Format("..{0}BookCategory", Path.DirectorySeparatorChar)));
}
});
}
}
}
修改Program.cs
using BookApp;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Host
.AddAppSettingsSecretsJson()
.UseAutofac();
await builder.AddApplicationAsync<BookAppWebModule>();
var app = builder.Build();
await app.InitializeApplicationAsync();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
await app.RunAsync();
新建資原始檔
新建BookCategory類庫專案
新建BookCategoryModule.cs
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
using Volo.Abp;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc;
using BookApp.Localization;
namespace BookCategory
{
[DependsOn(
typeof(AbpVirtualFileSystemModule),
typeof(AbpAspNetCoreMvcModule)
)]
public class BookCategoryModule: AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var hostingEnvironment = context.Services.GetHostingEnvironment();
var configuration = context.Services.GetConfiguration();
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<BookCategoryModule>();//新增程式集到虛擬檔案系統
if (hostingEnvironment.IsDevelopment())
{
}
});
Configure<AbpLocalizationOptions>(options =>
{
options.Resources
.Add<BookCategoryResource>("en")
.AddVirtualJson("/Localization/BookCategory"); //這裡必需新增,不然本地化時找不到相應的json檔案
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
}
}
}
新建BookCategoryResource.cs
using Volo.Abp.Localization;
namespace BookApp.Localization;
[LocalizationResourceName("BookCategory")]
public class BookCategoryResource
{
}
新建資原始檔
修改mvc專案的HomeController.cs中的Privacy方法
public IActionResult Privacy()
{
var resourcePath = "/Localization/BookCategory/en.json";
var fileInfo = _fileProvider.GetFileInfo(resourcePath);
if (fileInfo.Exists)
{
using (var stream = fileInfo.CreateReadStream())
using (var reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
return Content(content);
}
}
return Content("Resource not found");
}
此時我們就可以訪問BookCategory類庫中的資原始檔
修改mvc專案中的檢視Index.cshtml
@using Microsoft.Extensions.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using BookApp.Controllers
@using BookApp.Localization
@inject IHtmlLocalizer<BookStoreResource> HtmlLocalizer
@inject IStringLocalizer<BookCategoryResource> StringLocalizer
@{
ViewData["Title"] = "Home Page";
}
<div>string: @StringLocalizer["AppName"]</div>
<div>html: @HtmlLocalizer["AppName"]</div>
IStringLocalizer
作者
吳曉陽(手機:13736969112微信同號)