在单独的项目Asp.net Core MVC中进行本地化
我刚刚升级到Rc2,而以前的工作不再适用。 我在一个单独的项目中有几个resx文件,我使用自定义类来访问数据。 现在运行时出现以下错误:
MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源。 确保在编译时将“GarageWeb.Core.CoreResources.resources”正确嵌入或链接到程序集“GarageWeb.Core”中,或者所有所需的附属程序集都是可加载和完全签名的。
编辑:我简化了这一点,并创建了一个控制台应用程序,除了重现错误所需的一切,所以除去了所有内容: https : //github.com/GarageWeb/ResourceTest
这是访问资源的类:
public class ResourceService : IResourceService { private readonly ILoggingService _loggingService; private readonly ICoreGlobalResourceService _coreGlobalResources; private readonly ISiteGlobalResourceService _siteGlobalResources; public ResourceService(ILoggingService loggingService, ICoreGlobalResourceService coreGlobalResourceService, ISiteGlobalResourceService siteGlobalResources) { _loggingService = loggingService; _coreGlobalResources = coreGlobalResourceService; _siteGlobalResources = siteGlobalResources; } public string GetGlobalText(string resourceKey, bool includeBrackets = true) { var localizedString = _coreGlobalResources.ResourceManager.GetString(resourceKey); if (string.IsNullOrEmpty(localizedString)) { localizedString = _siteGlobalResources.ResourceManager.GetString(resourceKey); } if (string.IsNullOrEmpty(localizedString) && includeBrackets) { _loggingService.LogInvalidResource(resourceKey); } if (includeBrackets) { return localizedString ?? "[" + resourceKey + "]"; } return localizedString ?? resourceKey; } public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1, bool includeBrackets = true) { var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets), GetGlobalText(placeHolderResourceKey1, includeBrackets)); return errorString; } public string BuildMessageFromResourceAndArray(string resourceKey, string[] arrayOfValues, bool includeBrackets = true) { var placeHolderValue = ""; for (var i = 0; i < arrayOfValues.Length; i++) { if (i + 1 == arrayOfValues.Length) { placeHolderValue += GetGlobalText(arrayOfValues[i], includeBrackets); } else { placeHolderValue += GetGlobalText(arrayOfValues[i], includeBrackets) + ", "; } } var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets), placeHolderValue); return errorString; } public string BuildMessageFromResourceAndTwoArrays(string resourceKey, string[] firstArrayOfValues, string[] secondArrayOfValues, bool includeBrackets = true) { var placeHolderOneValue = ""; var placeHolderTwoValue = ""; for (var i = 0; i < firstArrayOfValues.Length; i++) { if (i + 1 == firstArrayOfValues.Length) { placeHolderOneValue += GetGlobalText(firstArrayOfValues[i], includeBrackets); } else { placeHolderOneValue += GetGlobalText(firstArrayOfValues[i], includeBrackets) + ", "; } } for (var i = 0; i < secondArrayOfValues.Length; i++) { if (i + 1 == secondArrayOfValues.Length) { placeHolderTwoValue += GetGlobalText(secondArrayOfValues[i], includeBrackets); } else { placeHolderTwoValue += GetGlobalText(secondArrayOfValues[i], includeBrackets) + ", "; } } var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets), placeHolderOneValue, placeHolderTwoValue); return errorString; } public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1, string placeHolderResourceKey2, bool includeBrackets = true) { var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets), GetGlobalText(placeHolderResourceKey1, includeBrackets), GetGlobalText(placeHolderResourceKey2, includeBrackets)); return errorString; } public string BuildMessageFromResource(string resourceKey, string placeHolderResourceKey1, string placeHolderResourceKey2, string placeHolderResourceKey3, bool includeBrackets = true) { var errorString = string.Format(CultureInfo.CurrentCulture, GetGlobalText(resourceKey, includeBrackets), GetGlobalText(placeHolderResourceKey1, includeBrackets), GetGlobalText(placeHolderResourceKey2, includeBrackets), GetGlobalText(placeHolderResourceKey3, includeBrackets)); return errorString; } }
它失败了:var localizedString = _coreGlobalResources.ResourceManager.GetString(resourceKey);
有任何想法吗? 有没有新方法来嵌入这些资源?
因此,如果我将.resx文件移动到项目的根目录而不是子文件夹中,它将按预期工作。 我已尝试从子文件夹嵌入的各种方式,它不再有效。 现在我将使用此解决方法,但我怀疑这是RC2中的一个错误。
就像你提到的,我也相信这是一个错误。 我想这么久,该错误位于自动生成设计器工具中。 由于奇怪的原因,以及为什么您的解决方法有效,该工具假定您将所有resx文件放在应用程序的根目录中。
以下是该工具输出的示例:
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] public static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Cosmos.ViewModels.Default", typeof(Default).GetTypeInfo().Assembly); resourceMan = temp; } return resourceMan; } }
其中, Cosmos.ViewModels
是程序集名称,但.resx文件都收集在(对于我们当前的解决方案) Resources.da
名称空间中,提供了Cosmos.ViewModels.Resources.da
的完全限定名称空间。
考虑到这一点,您可以将文件保留在您希望的位置,然后将Cosmos.ViewModels.Default
的硬编码字符串Cosmos.ViewModels.Default
为Cosmos.ViewModels.Resources.da.Default
。
当然,工具可能会意外地再次删除您的更改。
我希望微软能解决这个问题。 我在github上创建了一个问题,但我认为问题实际上应该在核心cli中。
包含程序集中的MissingManifestResourceException尽管存在#1534
我正在使用MVC Core 1.1.0,我能够在子文件夹中添加资源文件。 当您尝试更改没有子文件夹的资源文件的命名空间时,它会抛出错误。 因此资源文件命名空间cane与生成时相同。
- 将Web服务迁移到.NET Core 2.0并返回json
- unit testing.NET Standard 1.6库
- .NET Core 2.0日志记录是否已损坏?
- 如何在asp.net核心中编写自定义actionResult
- 如何在.Net Core类库中使用Rijndael加密? (不是.Net Framework)
- .Net Standard中CompileToMethod的替代品
- Microsoft.DotNet.Common.Targets(262,5):错误:对象引用未设置为对象的实例
- .NETentity framework核心
- .NET Core 2.0模块化dependency injection