Skip to content

How to create templates for ASP.NET Core

Tyler Hughes edited this page Feb 3, 2016 · 2 revisions
  1. Create your ASP.NET Core project and your VSIX project.
  2. Right click your VSIX project and go to Add -> Add Template Reference (SideWaffle Project).
  3. Select your template project and click on ok. If you receive an error "Did not find new element [...]", simply ignore it.
  4. Modify your _project.vstemplate.xml file like so:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>Project Template Name</Name>
    <Description>Project Template Description</Description>
    <DefaultName>WebApplication</DefaultName>
    
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>Web</ProjectSubType>
    <RequiredFrameworkVersion>4.5.1</RequiredFrameworkVersion>
    <CreateInPlace>true</CreateInPlace>
    <PromptForSaveOnCreation>true</PromptForSaveOnCreation>
    
    <SortOrder>1001</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>sw-file-icon.png</Icon>
    <!-- Note you need to add a new sw-file-previewimage.png file here. -->
    <PreviewImage>sw-file-previewimage.png</PreviewImage>
    <TemplateID>02bfed6c-6c85-4135-8719-fc23440bd9db</TemplateID>
    <!-- Indicates how many parent folders this item template should appear in -->
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
  </TemplateData>
  <TemplateContent>
    <!-- Replace AspNetProject with the name of your project. -->
    <Project File="AspNetProject.xproj" ReplaceParameters="true">
      <!-- Opens a ReadMe.html file in my project template. -->
      <ProjectItem ReplaceParameters="true" TargetFileName="ReadMe.html" OpenInWebBrowser="true">ReadMe.html</ProjectItem>
    </Project>
  </TemplateContent>
</VSTemplate>

Note: the Project element should not have a TargetFileName attribute in this case.

  1. Modify your _preprocess.vstemplate.xml file like so:
<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <!--
  You can specify the path where this should show up in the
  Add New Project / Add New Item dialog by setting the value below
  -->
  <TemplateInfo Path="CSharp\Web"/>
  <Replacements Include="*.*" Exclude="ReadMe.html;*.vstemplate;*.csproj;*.fsproj;*.vbproj;*.jpg;*.png;*.ico;_preprocess.xml;_project.vstemplate.xml">
    <!-- Find and replace the root namespace with the generated one. -->
    <add key="AspNetProject" value="$safeprojectname$"/>
    <!-- Replace the ProjectGuid (Unique ID for the project) with a generated GUID. Replace this with your own. -->
    <add key="6e0ef33d-3c19-4ea2-8ca9-c7bf19bdd947" value="$guid1$"/>
    <!-- Replace the GUID in userSecretsId in the project.json file with a generated GUID. Replace this with your own if you are using userSecretsId. -->
    <add key="fe5dc3df-2725-4ccc-9e53-0fc56fe83882" value="$guid2$"/>
  </Replacements>
</Preprocess>

For a full example project you can refer to the xUnit project template or ASP.NET MVC Boilerplate.