c# - 当前文化不存在 list

标签 c# asp.net-core embedded-resource

自学 .Net Core asp。

我已将资源文件添加到我的解决方案中。 我已向该资源文件添加了一个测试字符串。 我现在正在尝试在我的 Controller 中访问此测试字符串/ key 。

所以,在我的startup.cs中我有这个:

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });

    services.AddMvc()
       .AddViewLocalization(
            LanguageViewLocationExpanderFormat.Suffix,
                opts => { opts.ResourcesPath = "Resources"; })
    .AddDataAnnotationsLocalization();

    services.Configure<RequestLocalizationOptions>(
        opts =>
        {
            var supportedCultures = new List<CultureInfo>
            {
                    new CultureInfo("en-GB"),
                    new CultureInfo("en-US"),
                    new CultureInfo("en"),
                    new CultureInfo("fr-FR"),
                    new CultureInfo("fr"),
            };

            opts.DefaultRequestCulture = new RequestCulture("en");
            // Formatting numbers, dates, etc.
            opts.SupportedCultures = supportedCultures;
            // UI strings that we have localized.
            opts.SupportedUICultures = supportedCultures;
        });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WorkerContext context)
{
    app.UseStaticFiles();

    var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(options.Value);

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

在我的 Controller 中我有这个:

public class HomeController : Controller
{
    private readonly IStringLocalizer<HomeController> _localizer;
    public HomeController(IStringLocalizer<HomeController> localizer)
    {
        _localizer = localizer;
    }
}

我的资源文件存储在这里:

enter image description here

我对此资源文件的属性是: enter image description here

我在这里设置了一个断点:

_localizer = 本地化器;

我检查了这个变量,如您所见,尚未找到 list ... enter image description here

我不明白什么?

最佳答案

问题 1

基于https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization

您应该将您的资源放在根目录下名为Resources的文件夹中。它位于 Properties 文件夹下...

此代码块(与您的略有不同)
Statup.cs

services.AddLocalization(options => options.ResourcesPath = "Resources");

在我的项目中解析为该文件夹。

enter image description here


问题 2

另一个问题是你的命名不正确。看一下名为 Working With Resource Files 的部分这比我把它放在这里更能向你解释它。

关于c# - 当前文化不存在 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44356050/

相关文章:

c# - 分发需要 Microsoft.Office.Interop.Excel 的应用程序

c# - 在没有 Visual Studio 的情况下在 C# 中嵌入资源

c - FindResource 工作,LoadBitmap 不工作,从磁盘工作的 LoadImage

c# - 主线程上的网络请求导致帧率延迟峰值

c# - 从 SandcaSTLe 生成的文档中隐藏从 Object 继承的方法?

c# - 从 .Net Core 2.0 中的 IActionFilter 获取 HttpStatus 代码

c# - 在 SignalR Hub 的 OnConnected/OnDisconnected 中注入(inject)租户信息

ASP.NET Core ApiVersioning 更改中间件层次结构

java - jPanel 的背景图像不工作

c# - 如何在asp.net <%= %> 脚本中使用javascript作为参数