.net core 无法访问.net Core 3中的resx文件
已将新的.Resx文件添加到.NETCoreWeb3项目下的Resources文件夹中 尝试访问resx文件时,其在控制器或验证属性上不可用.net core 无法访问.net Core 3中的resx文件,.net-core,resx,asp.net-core-3.0,.net-core-3.0,.net Core,Resx,Asp.net Core 3.0,.net Core 3.0,已将新的.Resx文件添加到.NETCoreWeb3项目下的Resources文件夹中 尝试访问resx文件时,其在控制器或验证属性上不可用 在.Net Core中使用Resx文件的正确方法是什么。要在Controller中配置本地化,可以参考以下步骤 1.在Startup.ConfigureServices方法中配置本地化,并在Startup.Configure方法中设置区域性。必须在任何可能检查请求区域性的中间件之前配置本地化中间件: public void ConfigureService
在.Net Core中使用Resx文件的正确方法是什么。要在Controller中配置本地化,可以参考以下步骤 1.在
Startup.ConfigureServices
方法中配置本地化,并在Startup.Configure
方法中设置区域性。必须在任何可能检查请求区域性的中间件之前配置本地化中间件:
public void ConfigureServices(IServiceCollection services)
{
//Adds the localization services to the services container. The code above also sets the resources path to "Resources"
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
2.ConfigureServices
方法将ResourcesPath
设置为“Resources”,因此主控制器的法语资源文件的项目相对路径为Resources/Controllers.HomeController.fr.resx
。或者,您可以使用文件夹来组织资源文件。对于主控制器,路径应为Resources/Controllers/HomeController.fr.resx
然后使用IStringLocalizer
,它使用ResourceManager和ResourceReader在运行时提供特定于区域性的资源来访问Controller中的Resx文件
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult About()
{
ViewData["Message"] = _localizer["Your application description page."];
return View();
}
}
公共类HomeController:控制器
{
专用只读IStringLocalizer\u localizer;
公共家庭控制器(IStringLocalizer)
{
_定位器=定位器;
}
公共IActionResult About()
{
ViewData[“Message”]=“本地化程序[“您的应用程序描述页];
返回视图();
}
}
有关更多详细信息,您可以参考包含示例代码和DataAnnotations本地化的。有关在Controller中配置本地化,您可以参考以下步骤 1.在
Startup.ConfigureServices
方法中配置本地化,并在Startup.Configure
方法中设置区域性。必须在任何可能检查请求区域性的中间件之前配置本地化中间件:
public void ConfigureServices(IServiceCollection services)
{
//Adds the localization services to the services container. The code above also sets the resources path to "Resources"
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures
});
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
2.ConfigureServices
方法将ResourcesPath
设置为“Resources”,因此主控制器的法语资源文件的项目相对路径为Resources/Controllers.HomeController.fr.resx
。或者,您可以使用文件夹来组织资源文件。对于主控制器,路径应为Resources/Controllers/HomeController.fr.resx
然后使用IStringLocalizer
,它使用ResourceManager和ResourceReader在运行时提供特定于区域性的资源来访问Controller中的Resx文件
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
public IActionResult About()
{
ViewData["Message"] = _localizer["Your application description page."];
return View();
}
}
公共类HomeController:控制器
{
专用只读IStringLocalizer\u localizer;
公共家庭控制器(IStringLocalizer)
{
_定位器=定位器;
}
公共IActionResult About()
{
ViewData[“Message”]=“本地化程序[“您的应用程序描述页];
返回视图();
}
}
有关更多详细信息,您可以参考包含示例代码和DataAnnotations本地化的。我确实尝试按照上面的答案中所述的方式进行操作,但由于太过困惑,似乎有很多事情要做。当我在寻找使用资源文件的方法时,就像我们在Asp.NETWebForms中所做的那样。我发现了一个不那么复杂的解决方案,即我们可以使用
Resource.ResourceManager.GetString(“Hello”,new CultureInfo(“Ar”)
其中,“Hello”是您从资源字符串中使用的字符串键,“Ar”是您希望处于活动状态的区域性。
这似乎不是最合适的方式,但对我来说很有效。我们可以从何处着手,将文化带到这里。我使用会话管理它。你可以从我这里得到参考资料。您也可以查看他们的示例代码。我确实尝试按照上面的答案中所述的方式进行操作,但却感到太困惑,似乎要做很多事情才能实现这一点。当我在寻找使用资源文件的方法时,就像我们在Asp.NETWebForms中所做的那样。我发现了一个不那么复杂的解决方案,即我们可以使用
Resource.ResourceManager.GetString(“Hello”,new CultureInfo(“Ar”)
其中,“Hello”是您从资源字符串中使用的字符串键,“Ar”是您希望处于活动状态的区域性。
这似乎不是最合适的方式,但对我来说很有效。我们可以从何处着手,将文化带到这里。我使用会话管理它。你可以从我这里得到参考资料。您也可以查看他们的示例代码。请,共享您的代码请,共享您的代码我们是否需要单独的视图,例如,
About.fr.cshtml
和About.en.cshtml
,验证将像基于属性的验证一样工作。否。关于视图本地化和数据注释本地化的资源文件的命名和使用,您可以参考我上面回复的最后一行中的官方文档链接。我们是否还需要单独的视图,例如,About.fr.cshtml
和About.en.cshtml
,验证工作如下,基于属性的验证。否。关于视图本地化和数据注释本地化的资源文件的命名和使用,您可以参考我上面回复的最后一行中的官方文档链接。