5

I'm going to create a custom Visual Studio project template to avoid repeating extra work of common configuration tasks for an ASP.NET Core project like adding EntityFramework with MySQL support. So I created a new sample project for this and did an export using Project > Export template.

But when I create a new project based on my custom template, all folders like Controllers or Views are missing. Only wwwroot exists. I inspected the .vstemplate file, which seems to adding the folders correctly

  <Folder Name="wwwroot" TargetFolderName="wwwroot" />
  <Folder Name="Controllers" TargetFolderName="Controllers">

So it's not understandable for me, that wwwroot is created, but Controllers not, since they're using the exactly same directive.

The full VS template file:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>AspNetCoreMySQL</Name>
    <Description>Test</Description>
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>AspNetCoreMySQL</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="AspNetCoreMySQL.csproj" File="AspNetCoreMySQL.csproj" ReplaceParameters="true">
      <Folder Name="Properties" TargetFolderName="Properties">
        <ProjectItem ReplaceParameters="true" TargetFileName="launchSettings.json">launchSettings.json</ProjectItem>
      </Folder>
      <Folder Name="wwwroot" TargetFolderName="wwwroot" />
      <Folder Name="Controllers" TargetFolderName="Controllers">
        <ProjectItem ReplaceParameters="true" TargetFileName="PageController.cs">PageController.cs</ProjectItem>
      </Folder>
      <ProjectItem ReplaceParameters="true" TargetFileName="Program.cs">Program.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="ScaffoldingReadMe.txt">ScaffoldingReadMe.txt</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="Startup.cs">Startup.cs</ProjectItem>
    </Project>
  </TemplateContent>
</VSTemplate>
Lion
  • 16,606
  • 23
  • 86
  • 148

1 Answers1

8

This nice workaround is actual for Visual Studio 2017. Edit .vstemplate and add:

<CreateInPlace>true</CreateInPlace>

inside:

<TemplateData>...</TemplateData>

Don't forget to copy updated ZIP file to:

Documents\Visual Studio 2017\Templates\ProjectTemplates\

This should reload the template in VS. Please pay attention to @Lion comment below if you have troubles with reloading.

Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
  • 2
    That works, **BUT** updating templates is a real mess in Visual Studio: My zip file in `Documents/Visual Studio 2017/Templates/ProjectTemplates` is completely ignored. Instead, the extracted folder in `C:\Users\Daniel\AppData\Roaming\Microsoft\VisualStudio\15.0_51c08c96\ProjectTemplatesCache` is used. So I've to update it there or delete the sub-folder for the template first. Such stupid behaviour cause a lot of trouble to make simple things work... But thanks for your solution, it works when you clear the cache first :) – Lion May 25 '17 at 06:33