简体   繁体   English

我可以将资源(resx)文件用作“嵌入式资源” *,也可以用作“内容”吗?

[英]Can I use a resource (resx) file as an “Embedded Resource” *as well as* as “Content”?

I have a resx file in the App_GlobalResources folder of my ASP.NET web application. 我的ASP.NET Web应用程序的App_GlobalResources文件夹中有一个resx文件。

  • Its build property must be set to "Content", so that the file is automatically included during publishing and I can use <%$ Resources: ... %> expressions. 它的build属性必须设置为“ Content”,以便在发布过程中自动包含该文件,并且我可以使用<%$ Resources: ... %>表达式。

  • Its build property must be set to "Embedded Resource", so that my unit tests can access the resources . 必须将其build属性设置为“ Embedded Resource”, 以便我的单元测试可以访问资源

Is there some trick that I can use to have both? 我可以同时使用一些技巧吗?


Note: The solutions in the linked question won't work, since 注意:链接的问题中的解决方案将不起作用,因为

  • mocking HttpContext won't give the unit test access to non-embedded resources, and 模拟HttpContext不会使单元测试访问非嵌入式资源,并且
  • moving the resources out of App_GlobalResources breaks <%$ Resources: ... %> support after deployment. 部署后将资源移出App_GlobalResources将中断<%$ Resources: ... %>支持。

Is there any solution that I have missed? 有什么我错过的解决方案吗?

Set the build property of your resource files to "Embedded Resource". 将资源文件的生成属性设置为“嵌入式资源”。

Then, to ensure that they are included when publishing, add the following to the bottom of your vbproj or csproj file: 然后,为确保在发布时将它们包括在内,请将以下内容添加到vbprojcsproj文件的底部:

<Target Name="CopyResx" AfterTargets="CopyAllFilesToSingleFolderForPackage">
  <ItemGroup>
    <ResxFilesToCopy Include="App_GlobalResources\*.resx" />
  </ItemGroup>
  <Copy SourceFiles="@(ResxFilesToCopy)" DestinationFolder="$(WebProjectOutputDir)\$(WPPAllFilesInSingleFolder)\App_GlobalResources" />
</Target>

Note: We need to use CopyAllFilesToSingleFolderForPackage instead of MSDeployPublish , since the latter won't be executed when publishing a web application to a file system directory . 注意:我们需要使用CopyAllFilesToSingleFolderForPackage而不是MSDeployPublish ,因为在将Web应用程序发布到文件系统目录中时 ,后者将不会执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM