我可以将资源 (resx) 文件用作 "Embedded Resource" *以及* "Content" 吗?
Can I use a resource (resx) file as an "Embedded Resource" *as well as* as "Content"?
我的 ASP.NET 网络应用程序的 App_GlobalResources 文件夹中有一个 resx 文件。
它的构建属性必须设置为"Content",以便在发布时自动包含该文件,我可以使用<%$ Resources: ... %>
表达式。
它的构建 属性 必须设置为 "Embedded Resource", so that my unit tests can access the resources.
我可以使用一些技巧来同时拥有两者吗?
注意:链接问题中的解决方案将不起作用,因为
- 模拟 HttpContext 不会为单元测试提供对非嵌入式资源的访问权限,并且
- 将资源移出 App_GlobalResources 会破坏
<%$ Resources: ... %>
部署后的支持。
有没有我遗漏的解决方案?
将资源文件的构建 属性 设置为 "Embedded Resource"。
然后,为确保在发布时包含它们,请将以下内容添加到 vbproj
或 csproj
文件的底部:
<Target Name="CopyResx" AfterTargets="CopyAllFilesToSingleFolderForPackage">
<ItemGroup>
<ResxFilesToCopy Include="App_GlobalResources\*.resx" />
</ItemGroup>
<Copy SourceFiles="@(ResxFilesToCopy)" DestinationFolder="$(WebProjectOutputDir)$(WPPAllFilesInSingleFolder)\App_GlobalResources" />
</Target>
注意:我们需要使用CopyAllFilesToSingleFolderForPackage
而不是MSDeployPublish
,因为后者是won't be executed when publishing a web application to a file system directory.
我的 ASP.NET 网络应用程序的 App_GlobalResources 文件夹中有一个 resx 文件。
它的构建属性必须设置为"Content",以便在发布时自动包含该文件,我可以使用
<%$ Resources: ... %>
表达式。它的构建 属性 必须设置为 "Embedded Resource", so that my unit tests can access the resources.
我可以使用一些技巧来同时拥有两者吗?
注意:链接问题中的解决方案将不起作用,因为
- 模拟 HttpContext 不会为单元测试提供对非嵌入式资源的访问权限,并且
- 将资源移出 App_GlobalResources 会破坏
<%$ Resources: ... %>
部署后的支持。
有没有我遗漏的解决方案?
将资源文件的构建 属性 设置为 "Embedded Resource"。
然后,为确保在发布时包含它们,请将以下内容添加到 vbproj
或 csproj
文件的底部:
<Target Name="CopyResx" AfterTargets="CopyAllFilesToSingleFolderForPackage">
<ItemGroup>
<ResxFilesToCopy Include="App_GlobalResources\*.resx" />
</ItemGroup>
<Copy SourceFiles="@(ResxFilesToCopy)" DestinationFolder="$(WebProjectOutputDir)$(WPPAllFilesInSingleFolder)\App_GlobalResources" />
</Target>
注意:我们需要使用CopyAllFilesToSingleFolderForPackage
而不是MSDeployPublish
,因为后者是won't be executed when publishing a web application to a file system directory.