前言
Volo.Abp.VirtualFileSystem
是ABP(ASP.NET Boilerplate)框架中的一个重要组件,它提供了一种抽象文件系统的方式,使得应用程序可以轻松地访问和管理文件资源,无论这些资源是来自于物理文件系统、嵌入资源,还是远程存储。
通过Volo.Abp.VirtualFileSystem
,开发者可以使用统一的接口来处理文件和目录,而不用关心这些文件和目录的实际存储位置。这使得应用程序更加灵活,可以轻松地切换不同的文件存储方式,而不用修改大量的代码。
新建mvc项目
引用以下nuget包
新建BookAppWebModule.cs
C#
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
C#
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
C#
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
c#
using Volo.Abp.Localization;
namespace BookApp.Localization;
[LocalizationResourceName("BookCategory")]
public class BookCategoryResource
{
}
新建资源文件
修改mvc项目的HomeController.cs中的Privacy方法
C#
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
C#
@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 StringLocalizer 这里就可以读取BookCategory类库中的资源文件
作者
吴晓阳(手机:13736969112微信同号)