Search code examples
c#asp.net-corerazorlocalizationblazor

Move Razor library resources to MVC published folder


I have a Razor Class library with a Razor component and a (Dutch/nl) localization resource (.resx) in a Resources folder. I use the component on a view in a MVC app in a separate project, using the <component> tag.

When I run my MVC app in Visual Studio, the localized strings for the component get retrieved correctly. However, when I publish the MVC app to {publishfolder}, the RazorComponentLibrary.resources.dll file for the component ends up in the {publishfolder}\wwwroot\_framework\_bin\nl\ folder and the localized strings cannot be retrieved. If I manually copy/move RazorComponentLibrary.resources.dll to the {publishfolder}\nl\ folder everything works fine again.

Is it possible to specify, in the csproj of the MVC app for example, that the resource.dll doesn't need to be placed in the wwwroot folder, but in the nl folder?

I already tried adding

<EmbeddedResource Include="full-path-to-resx-file" Link="Resources\RazorComponentLibrary.ComponentName.nl.resx" />

to the csproj of the MVC app, but this doesn't help.


Solution

  • Ok, I found the solution. I added the following tasks to the csproj file of my MVC app:

    <Target Name="CopyRazorResourceFilesNl" AfterTargets="Publish">
        <Copy SourceFiles="$(PublishDir)\wwwroot\_framework\_bin\nl\RazorComponentLibrary.resources.dll" DestinationFolder="$(PublishDir)\nl" />
    </Target>
    <Target Name="CopyRazorResourceFilesEn" AfterTargets="Publish">
        <Copy SourceFiles="$(PublishDir)\wwwroot\_framework\_bin\en\RazorComponentLibrary.resources.dll" DestinationFolder="$(PublishDir)\en" />
    </Target>
    

    This copies the dll in wwwroot to the nl (and en) folder that is used by the MVC app for localization. I need the dll in both locations, so this is perfect.

    You can find the reference for the Copy task here: https://learn.microsoft.com/en-us/visualstudio/msbuild/copy-task?view=vs-2019