NetCore Resource
Posted 云霄宇霁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NetCore Resource相关的知识,希望对你有一定的参考价值。
在netcore中resource的配置及应用
1、创建resource资源文件
2、在容器中添加配置
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); builder.Services.Configure<RequestLocalizationOptions>(options => var supportedCultures = new List<CultureInfo> new CultureInfo("en"), new CultureInfo("ja") ; options.DefaultRequestCulture = new RequestCulture(new CultureInfo("en")); options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; options.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(context => var defaultLanguage = "ja";//"en"; return Task.FromResult(new ProviderCultureResult(defaultLanguage, defaultLanguage))!; )); );
var requestLocalizationOptions = app.Services.GetService<IOptions<RequestLocalizationOptions>>(); app.UseRequestLocalization(requestLocalizationOptions!.Value);
3、添加测试controller
[ApiExplorerSettings(GroupName = "demo2")] [ApiController] [Route("[controller]")] public class ResourceController : Controller private readonly IStringLocalizer<ResourceDemo> _localizer; public ResourceController(IStringLocalizer<ResourceDemo> localizer) _localizer = localizer; [HttpGet("resourcedemo")] public IActionResult ResourceDemo() var value = _localizer.GetString("demo01"); return new JsonResult(value);
4、测试结果
资源文件内容,显示日语资源文件value是因为测试执行defaultlanguage 是 ja。
OK Resource 搞定!
以上是关于NetCore Resource的主要内容,如果未能解决你的问题,请参考以下文章
.net 核心中的 MissingManifestResourceException