简体   繁体   中英

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.

  • Its build property must be set to "Content", so that the file is automatically included during publishing and I can use <%$ Resources: ... %> expressions.

  • Its build property must be set to "Embedded Resource", so that my unit tests can access the resources .

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
  • moving the resources out of App_GlobalResources breaks <%$ Resources: ... %> support after deployment.

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:

<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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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