I think I must be missing some glaringly obvious step here. I’m trying to build a VSIX template project for a C# .NET 8 executable. It works, all except the parameter replacement.
I have the following file in my template project:
DependencyInjection.cs (BuildAction: None, Copy to Output Directory: Do not copy)
using Microsoft.Extensions.DependencyInjection;
using $rootnamespace$.Repositories;
namespace $rootnamespace$;
public static class DependencyInjection
{
}
And I have the following line in my vstemplate file:
<ProjectItem ReplaceParameters="true" TargetFileName="DependencyInjection.cs">DependencyInjection.cs</ProjectItem>
However, when I use the template, I get the following DependencyInjection.cs file made for me, which is… not right, but not untouched. Looks like the token replacer did something, but not what I was hoping for.
using $
using Microsoft.Extensions.DependencyInjection;
rootnamespace$.Repositories;
namespace $rootnamespace$;
public static class DependencyInjection
{
}
What step did I miss? I’ve tried this with $defaultnamespace$
as well and got similar results. I feel like this is supposed to just work, so I must be missing something super obvious.
I followed your steps and tested at my side and found VSIX Template Parameters are replaced correctly. You can check the following steps to see if you missed something.
1.Have the file DependencyInjection.cs
in your template project. Modify the code file to indicate where parameter replacement should take place.
2.On the Project menu, choose Export Template.
3.On the Choose Template Type page, choose Item Template, select the project that contains the item, and then choose Next.
4.Choose the item you want to create a template for, and then choose Next.
5 Have the following line in the vstemplate file:
<ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">DependencyInjection.cs</ProjectItem>
Test result:
For more information, please read docs :
Template parameters
Create Templates
2
After many hours of trial and error, I was able to identify the problem (no thanks to Microsoft’s docs).
The article on Template Parameters explicitly states
The following table lists the reserved template parameters that can be used by any template
That’s not the case though. Those templates parameters do not work for any template. Parameters which reference values that can only be known if a project is already existed, like $rootnamespace$
or $defaultnamespace$
don’t seem to work for items inside a Project Template. They only work for Item Templates when adding items to existing projects.
I solved my issue by using $safeprojectname$
as the parameter instead.