.netasp.net-corevisual-studio-2022blazor-webassemblyvisual-studio-templates

Template parameters not being replaced in certain files for multi-project template (.NET 8, ASP.NET Core, VS 2022)


My Scenario

I have the following ASP.NET Core Hosted Blazor WASM project:

enter image description here

I am trying to export it as a multi-project template. To do this, I do the following:

  1. Find/Replace all instances of my current project name with $ext_safeprojectname$.
  2. Export each project as a Template individually.
  3. Extract the files from the resulting .zip folders and place in a new directory.
  4. Add a MyTemplate.vstemplate file to the root of the new folder.

Here is the resulting folder structure:

enter image description here

Here are the contents of the .vstemplate file:

<VSTemplate Version="2.0.0" Type="ProjectGroup"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>My Wasm Template</Name>
        <Description>...</Description>
        <Icon>Icon.ico</Icon>
        <ProjectType>CSharp</ProjectType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="$safeprojectname$.Client" CopyParameters="true">
                MyWasmTemplate.Client\MyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="$safeprojectname$.Server" CopyParameters="true">
                MyWasmTemplate.Server\MyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="$safeprojectname$.Data" CopyParameters="true">
                MyWasmTemplate.Data\MyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="$safeprojectname$.Shared" CopyParameters="true">
                MyWasmTemplate.Shared\MyTemplate.vstemplate
            </ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
</VSTemplate>

Result

When I create a new project using this template, the new project name correctly replaces most of the $ext_safeprojectname$ instances, except the following files in the client project:

_Imports.razor:

@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.JSInterop
@using $ext_safeprojectname$.Client
@using $ext_safeprojectname$.Client.Layout
@using $ext_safeprojectname$.Shared
@using MudBlazor

MainLayout.razor:

...
<PageTitle>$ext_safeprojectname$</PageTitle>
...
    <p class="text-nowrap">$ext_safeprojectname$</p>
...

and one other blazor component:

@page "/blogs"
@using $ext_safeprojectname$.Shared.Dtos
@inject HttpClient Http

Why are these template parameters not being replaced in these files? They're being replaced everywhere else properly, even within the client project.


Solution

  • This was driving me crazy until I found this : https://developercommunity.visualstudio.com/t/Export-Template-is-not-replacing-the-pro/10730424?sort=newest&topics=visual Seems like there is bug in the template generation. If you open the vstemplate of your client project, you'll probably see that the razor files have ReplaceParameters="false". Just set it to true and it should work properly.