我有两个ASP。NET Web项目(ProjectA和projecb)。当ProjectA中的类正在实例化ProjectB中的一个类时,它使用了一个资源文件Blah。resx,我得到这个错误:

一个System.Resources类型的异常。mscorlib.dll中出现了MissingManifestResourceException',但在用户代码中没有处理。 无法找到适用于指定区域性或中性区域性的任何资源。确保“资源。等等。”资源“正确嵌入或链接到程序集”App_GlobalResources。Sn_flri6”,或者所有所需的附属程序集都是可加载的并具有全符号。

是什么导致的?

在微软的网站上有一篇关于http://support.microsoft.com/kb/318603的文章,它建议:

要解决此问题,请移动所有其他类定义,使它们出现在表单的类定义之后。

这是一个Windows窗体项目的解决方案,我不确定这是否也适用于Web项目。


当前回答

对我来说是这样的。在MyStrings.Designer.cs文件中:

global::System.Resources.ResourceManager temp = 
    new global::System.Resources.ResourceManager(
      "MyApplication.MyProject", typeof(MyStrings).Assembly);

我以为MyApplication。MyProject引用类的命名空间,而不是文件的目录。我把文件放在一个名为Resources的子目录中,但名称空间没有反映这一点。

我把它改成

global::System.Resources.ResourceManager temp = 
    new global::System.Resources.ResourceManager(
      "MyApplication.MyProject.Resources", typeof(MyStrings).Assembly);

它成功了!(我甚至不需要更新名称空间!)

其他回答

This can be caused by mismatched namespaces. The second from top answer (Sibi Elango's) says to right-click the resx file, and change Build option to EmbeddedResource, but I had already done that and still had the error. The top answer (CFinck's) notes a way of fixing this via manually editing files, however, I had this problem in MonoDevelop, and had to set the default namespace to the same as the cs file which was calling for the resource (the file which contained code such as the code below)...

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

通过GUI设置默认名称空间后,上面的代码行不再引起异常。

当我尝试共享资源时。resx文件从一个c#项目到另一个c#项目,我得到了这个问题。将Form类移到其文件开头的建议是不合适的。我就是这样解出来的。您实际上使用了从第二个项目到第一个项目的链接,然后启用resource.designer.cs文件的再生。

删除第二个项目的Properties/Resources。resx文件 添加第一个项目的Properties/Resources。resx文件作为第二个项目中Properties文件夹的链接。不要将它添加到项目的根级别。 不要添加第一个项目的Properties/Resources.designer.cs! 关于第二个项目的资源的属性。ResXFileCodeGenerator作为CustomTool 右键单击资源。resx,然后选择“运行自定义工具”。这将生成一个新的designer.cs文件。

注意:我将避免编辑resource.designer.cs文件,因为这是自动生成的。

对于在。net Core 3.0中遇到这个问题的用户,这可能与。net Core 3.0中的一个破坏性更改有关,要解决这个问题,只需在项目csproj中将EmbeddedResourceUseDependentUponConvention设置为false:

<PropertyGroup>
  <EmbeddedResourceUseDependentUponConvention>true</EmbeddedResourceUseDependentUponConvention>
</PropertyGroup>

我也面临着同样的问题,尝试了答案中提到的所有解决方案,但似乎都不起作用。结果在将代码签入TFS期间。TFS没有签入Resx文件,它只签入了设计器文件。因此,所有其他开发人员在他们的机器上运行时都面临着这个问题。手动签入resx文件就可以了

在一个NetCore项目中有类似的问题,我注意到在我添加了我的迁移(让我们将其命名为WhateverMigration)后,作为EmbeddedResource的.resx文件引用没有添加到.csproj中,即使一切看起来都很好。所以我打开我的。csproj文件,添加:

<EmbeddedResource Update="Migrations\202107071914322_WhateverMigration.resx">
    <Generator></Generator>
    <DependentUpon>202107071914322_WhateverMigration.cs</DependentUpon>
</EmbeddedResource>