Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What settings should be used to get custom ItemTemplates working with .NET Core projects?

I have a Visual Studio 2017 item template extension that is currently working with ASP.NET projects. It has the following .vstemplate:

<VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" xmlns:sdk="http://schemas.microsoft.com/developer/vstemplate-sdkextension/2010">
  <TemplateData>
    <Name>Angular Component</Name>
    <Description>Files for an Angular component</Description>
    <RequiredFrameworkVersion>4.5</RequiredFrameworkVersion>
    <Icon>AngularComponentTemplate.ico</Icon>
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>Web</ProjectSubType>
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$.component.html">base.component.html</ProjectItem>
    <ProjectItem ReplaceParameters="true" TargetFileName="$fileinputname$.component.ts">base.component.ts</ProjectItem>
  </TemplateContent>
</VSTemplate>

Additionally the VSIX file referencing it has

ItemTemplates\CSharp\Web

set for the "VSIX Sub Path" property of the project.

I cannot, however, get this template to appear in ASP.NET Core projects. I tried using this in the vstemplate:

<ProjectType>DNX</ProjectType>
<TemplateGroupID>SharedDotNetAndDotNetWeb</TemplateGroupID>

(from https://stackoverflow.com/a/38543920/1783619) but it didn't work. In the released version of Visual Stuido 2017, how do I get item templates to appear in .NET core projects?

like image 863
BradleyDotNET Avatar asked May 17 '17 16:05

BradleyDotNET


People also ask

Where are dotnet templates stored?

By default, user templates are located in: %USERPROFILE%\Documents\Visual Studio 2019\Templates\ProjectTemplates.

What is a template in Visual Studio?

These templates provide a starting point for users to begin creating projects, or to expand existing projects. Project templates provide the files that are required for a particular project type, include standard assembly references, and set default project properties and compiler options.

What are templates in. net?

Project templates produce ready-to-run projects that make it easy for users to start with a working set of code. . NET includes a few project templates such as a console application or a class library.


2 Answers

I had this same issue while creating an item template for pug files.

Here is what I did:

Firstly, I found out how the stock templates are written in:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\AspNetCore

This is how my template looks:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
  <TemplateData>
    <DefaultName>page.pug</DefaultName>
    <Name>Pug File</Name>
    <Description>This is a basic pug file template (previously Jade)</Description>
    <ProjectType>CSharp</ProjectType>
    <TemplateGroupID>AspNetCore</TemplateGroupID>
    <TemplateID>AspNetCore.Pug</TemplateID>
    <NumberOfParentCategoriesToRollUp>2</NumberOfParentCategoriesToRollUp>
    <Icon>__TemplateIcon.ico</Icon>
    <PreviewImage>__PreviewImage.ico</PreviewImage>
    <SortOrder>10</SortOrder>
    <ShowByDefault>false</ShowByDefault>
  </TemplateData>
  <TemplateContent>
    <References />
    <ProjectItem ReplaceParameters="true">page.pug</ProjectItem>
  </TemplateContent>
</VSTemplate>

Areas to pay attention to: ProjectType, TemplateGroupId, TemplateId(make up your own) and NumberOfParentCategoriesToRollUp

I wanted my template to show up mainly in the "Content" category, where the HTML and CSS type templates were so I placed my zip file in

Visual Studio 2017\Templates\ItemTemplates\Visual C#\AspNetCore\Web\Content

I had to create a few directories (AspNetCore\Web\Content) to get this structure.

Then the setting "NumberOfParentCategoriesToRollUp" set to 2 allows the template to display in the "Content" category, but also "roll up" and show in Web and AspNetCore categories.

More info on organization of templates here:

https://docs.microsoft.com/en-us/visualstudio/ide/how-to-locate-and-organize-project-and-item-templates

Then when you are all done, open a visual studio 2017 developer command prompt and type:

devenv /updateconfiguration
like image 119
Jason Tarr Avatar answered Oct 02 '22 09:10

Jason Tarr


The most important thing is

<TemplateGroupID>AspNetCore</TemplateGroupID>

Otherwise you will not see your item template from ASP.NET core project.

Strange thing is that you will see your item template with other project (desktop for example).

Hoping that Microsoft will document or make things easier one day

like image 38
Michel LAPLANE Avatar answered Oct 02 '22 10:10

Michel LAPLANE