Creating Visual Studio Custom Templates for ASP.NET Core Razor Pages
Create Visual Studio Custom Project Templates for ASP.NET Core Razor Pages Web Application
Traditionally Visual Studio comes with several default templates. There are two types of templates: Project templates, and item templates. Using those templates we build more complete projects for our own business use. Sometimes we want to create more specific types of templates. For example, if we have a customized project and we want to use it to create projects more often, Visual Studio provides a way to store them as a template and reuse them when creating a project.
Apart from this now with the new Dotnet core, there is a new set of templates available for Visual Studio. .Net core templates can be used either by Visual Studio & Visual Studio for Mac or the dotnet command line (dot net new).
Dotnet core templates are introduced in Visual Studio 2017. Here I am documenting how to create custom .core web app project templates for VS2017.
DotNet Core Web App Project Template for 2017
Things you need :
A copy of the fully customised project.
Take a screenshot of the running application, so that we can use it for the preview image for the project. It will show in the "New Project" window of the template wizard. and save it as a picture file, for example; PreviewImage.jpg.
A .ico file for the project to display in the "New ASP.NET Core Web Application" window.
Summary
This guide shows how to create a Visual Studio Project template for ASP.NET Core Web Application. It consists of two parts. Fist part creates the NuGet package and then creates a Visual Studio Extension Project (VSIX) to deploy the project to install into the computer.
These files will be configured, created or generated during this guide:
- template.json
- vs-2017.3.host.json
- .nuspec file (RazorTemplate.nuspec)
- .nupkg file (RazorTemplate.DACompany.1.1.0.nupkg)
- template.pkgdef
- source.extension.vsixmanifest
- .vsix file (RazorTemplate.vsix)
I will use the project created in the Getting Started With Web application using ASP.NET Core 2.2 in the Visual Studio 2017 guide.
Step 1: Open a new Visual Studio 2017 instance, select Create a new Project
Step 2: Then select the VSIX Project template found under the Extensibility node at the left.
Preparing the project for the Template
- You can use your existing custom project, MyCustomProject1. But it will not fit the wizard's "New ASP.NET Core Web Application" window options. For example, if the MyCustomProject1 does not have user authentication, when using this project template, even if the user selects the "Change Authentication" button to add individual user authentication, the final project created by your template does not add a user authentication feature to it.
Also if the MyCustomProject1 does have user authentication and when using this project template, even if the user didn't press the "Change Authentication" button and left it as "No Authentication" the final project created by your template adds a user authentication feature related to code to it. Therefore you don't need to use this feature. Instead, you can use the Export Template option from the Visual Studio IDE. - You get the original source code from the dotnetcore GitHub repository and make changes to either your custom project MyCustomProject1 or make changes to the downloaded code. It will not be a big task, and it will give users full wizard functionality in the "New ASP.NET Core Web Application" window options.
- Method-1: Make changes to MyCustomProject1 based on the source code project
- Method-2: Make changes to the source code project based on MyCustomProject1
Step 4:
Method-1: Make changes to the custom project based on the source code project
- Copy .template.config, Areas folder, Data folder and app.db file from the source code RazorPagesWeb-CSharp to MyCustomProject1 root folder.
- Copy Pages/Shared/_LoginPartial.Identity.cshtml and Pages/Shared/_LoginPartial.OrgAuth.cshtml found in source code RazorPagesWeb-CSharp to your MyCustomProject1's Pages/Shared folder.
- Compare MyCustomProject1s folder structure with downloaded RazorPagesWeb-CSharp folder structure using Windows Explorer and add all extra or missing files and folders from RazorPagesWeb-CSharp to MyCustomProject1. DO NOT replace any folder or file.
Comparing the files
@*#if (IndividualAuth || OrganizationalAuth) <partial name="_LoginPartial" /> #elseif (WindowsAuth) <p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p> #endif*@
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse" >
element, so the file would look like this:<header> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"> <div class="container"> <a class="navbar-brand" asp-area="" asp-page="/Index">Company.WebApplication1</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse"> @*#if (IndividualAuth || OrganizationalAuth) <partial name="_LoginPartial" /> #elseif (WindowsAuth) <p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p> #endif*@ <ul class="navbar-nav flex-grow-1"> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a> </li> <li class="nav-item"> <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a> </li> </ul> </div> </div> </nav> </header>
@*#if (IndividualLocalAuth)
@using Microsoft.AspNetCore.Identity
#endif*@
@using Company.WebApplication1
@*#if (IndividualLocalAuth)
@using Company.WebApplication1.Data
#endif*@
@namespace Company.WebApplication1.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Method-2: Make changes to the source code project based on your custom project
- Compare MyCustomProject1s folder structure with downloaded RazorPagesWeb-CSharp folder structure using Windows Explorer and add all extra files and folders from MyCustomProject1 to source code RazorPagesWeb-CSharp. DO NOT replace any folder or file.
- Delete the wwwroot folder located inside RazorPagesWeb-CSharp.
- Copy the entire wwwroot folder from MyCustomProject1 and paste it to the root of the RazorPagesWeb-CSharp.
Step 5:
Configuring Template
Step 6: Setting up the JSON configuration files
- template.json
- vs-2017.3.host.json
template.json
"author": "Microsoft",
"name": "ASP.NET Core Web App",
"description": "A project template for creating an ASP.NET Core application with example ASP.NET Razor Pages content",
"groupIdentity": "Microsoft.Web.RazorPages",
"identity": "Microsoft.Web.RazorPages.CSharp.2.2",
"shortName": [
"webapp",
"razor"
],
"sourceName": "Company.WebApplication1",
"author": "DACompany",
"name": "ASP.NET Core 2.2 - Razor Pages Web App",
"description": "A project template for creating an ASP.NET Core application with additional example ASP.NET Razor Pages content",
"groupIdentity": "DACompany.Web.RazorPages",
"identity": "DACompany.Web.RazorPages.CSharp.2.2",
"shortName": [
"rzp22",
"rzp22webapp"
],
"sourceName": "Company.WebApplication1",
vs-2017.3.host.json
"name": {
"text": "Web Application",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC9}",
"id": "1017"
},
"description": {
"text": "A project template for creating an ASP.NET Core application with example ASP.NET Razor Pages content.",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC9}",
"id": "1018"
},
"order": 300,
"icon": "vs-2017.3/WebApplication.png",
"learnMoreLink": "https://go.microsoft.com/fwlink/?LinkID=784881",
"name": {
"text": "Core 2.2 Razor Pages Web Application",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC8}",
"id": "221017"
},
"description": {
"text": "A project template for creating an ASP.NET Core 2.2 web application with extra example ASP.NET Razor Pages content.",
"package": "{0CD94836-1526-4E85-87D3-FB5274C5AFC8}",
"id": "221018"
},
"order": 300,
"icon": "vs-2017.3/icon.ico",
"learnMoreLink": "https://docs.ourwebsite.com/fwlink/?LinkID=784881",
- the name: text property value from this file will appear in the number 1, marked box area.
- the description: text property value from this file will appear in the number 2, marked box area.
- the icon property value from this file will show the icon in the number 1, marked box area
Step 7. Creating NuGet specification ( .nuspec) file
nuget spec
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<!-- <icon>icon.png</icon> -->
<projectUrl>http://project_url_here_or_delete_this_line/</projectUrl>
<description>$description$</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>$copyright$</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>RazorTemplate.DACompany</id>
<version>1.1</version>
<title>ASP.NET Core 2.2 Razor Web Templates package </title>
<authors>DACompany</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<icon>template.png</icon>
<description>New custom project template 2.2</description>
<releaseNotes>This provide additional Razor page template.</releaseNotes>
<copyright>$copyright$ 2018 DACompany</copyright>
<tags>asp.net-core-2.1 asp.net-core-2.2 Razor</tags>
</metadata>
<files>
<file src="template.png" target="" />
<file src="..\Company.WebApplication1\**\*.*" exclude="**\bin\**\*;**\obj\**\*;**\launchSettings.json;**/*.user" target="content\RazorPages.App22" />
</files>
</package>
Step 8. Generating the Nuget package (.nupkg ) file
nuget pack RazorTemplate.nuspec
Step 9. Create template.pkgdef file
The preferred way to register packages in Visual Studio is by using .pkgdef files. This allows for extension deployment without having to access the system registry, which is a requirement for VSIX deployment. Pkgdef files are created by using the CreatePkgDef Utility.
[$RootKey$\TemplateEngine\Templates\RazorPageTemplate\1.1]
"InstalledPath"="$PackageFolder$"
Step 10. Configuring the VSIX project
- Add new asset .nupkg file:
Click on the "New" tab to add a new asset. For the type, field give a type such as DACompay.RazorTemplate.NugetPackage, then for the source select, the file system, then use the browse button to locate the .nupkg file. In this guide, it is RazorTemplate.DACompany.1.1.0.nupkg file. - Add new asset .pkgdef file:
Click on the "New" tab to add a new asset. For the type field select Microsoft.VisualStudio.VsPackage from the drop-down, then for the source select, the file system, then use the browse button to locate the .pkgdef file. In this guide, it is template.pkgdef file.
Comments
Post a Comment