You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core特性文件夹结构下资源使用及视图Localizer适配问询

嘿,针对你在特性文件夹结构下遇到的资源和Localizer问题,我来给你梳理下可行的解决方案:

一、特性文件夹结构中资源的组织与使用

特性文件夹的核心是按业务功能聚合代码,资源文件也应该遵循这个原则,建议每个特性目录下单独维护自己的资源:

  • 每个特性文件夹(比如Features/Order)内,可以直接将资源文件和对应代码文件放在同一目录,或者创建Resources子文件夹统一存放:
    Features/
    ├─ Order/
    │  ├─ Controllers/
    │  │  ├─ OrderController.cs
    │  │  └─ OrderController.en.resx
    │  ├─ Models/
    │  │  ├─ OrderViewModel.cs
    │  │  └─ OrderViewModel.zh-CN.resx
    │  └─ Views/
    │     ├─ Index.cshtml
    │     └─ Index.en.resx
    
  • 务必将所有.resx文件的生成操作设置为EmbeddedResource(右键文件→属性→生成操作),确保资源被嵌入程序集供Localizer读取。
二、解决视图Localizer无法检索特性文件夹资源的问题

默认的IViewLocalizer会按标准MVC的Views/[Controller]/[View].resx路径查找资源,切换到特性文件夹后路径不匹配就会失效,这里有两种可靠的解决方案:

方案1:使用特性视图专属的IStringLocalizer

这种方法最直接,无需修改过多配置:

  1. 在视图中注入对应特性视图类型的IStringLocalizer,替代通用的IViewLocalizer:
    @* Features/Order/Views/Index.cshtml *@
    @inject IStringLocalizer<Features.Order.Views.Index> Localizer
    
    <h2>@Localizer["OrderTitle"]</h2>
    <p>@Localizer["OrderDescription"]</p>
    
  2. 确保资源文件名和视图的完全限定名匹配,比如Features/Order/Views/Index.en.resx,Localizer会自动定位到对应资源。

方案2:自定义StringLocalizerFactory适配特性文件夹结构

如果你想继续使用IViewLocalizer(保持和标准MVC一致的写法),可以通过自定义工厂修改资源查找逻辑:

  1. 创建自定义工厂类,继承ResourceManagerStringLocalizerFactory:
    public class FeatureFolderStringLocalizerFactory : ResourceManagerStringLocalizerFactory
    {
        public FeatureFolderStringLocalizerFactory(IOptions<LocalizationOptions> localizationOptions, ILoggerFactory loggerFactory)
            : base(localizationOptions, loggerFactory)
        {
        }
    
        protected override string GetResourcePrefix(Type resourceSource)
        {
            // 处理特性文件夹下的视图类型,将命名空间转换为资源路径
            var nsParts = resourceSource.Namespace.Split('.');
            if (nsParts.Length >= 3 && nsParts[0] == "Features")
            {
                // 将 Features.Order.Views.Index 转换为 Features/Order/Views/Index
                return string.Join("/", nsParts);
            }
            // 其他类型沿用默认逻辑
            return base.GetResourcePrefix(resourceSource);
        }
    }
    
  2. 在Program.cs中注册自定义工厂,替换默认实现:
    var builder = WebApplication.CreateBuilder(args);
    
    // 注册本地化服务,指定资源根路径为Features
    builder.Services.AddLocalization(options => options.ResourcesPath = "Features");
    
    // 替换为自定义的Localizer工厂
    builder.Services.AddSingleton<IStringLocalizerFactory, FeatureFolderStringLocalizerFactory>();
    
    builder.Services.AddControllersWithViews()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
    
  3. 现在你可以在特性视图中继续使用@inject IViewLocalizer Localizer,它会自动查找特性文件夹下的对应资源文件。
额外注意事项
  • 确保在Program.cs中配置了正确的请求文化中间件:
    app.UseRequestLocalization(options =>
    {
        var supportedCultures = new[] { "en", "zh-CN" };
        options.SetDefaultCulture("en")
               .AddSupportedCultures(supportedCultures)
               .AddSupportedUICultures(supportedCultures);
    });
    
  • 资源文件命名要符合规范:比如Index.en.resx(英文)、Index.zh-CN.resx(简体中文),避免拼写错误。

内容的提问来源于stack exchange,提问作者sindrem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:45:57