Creating a custom Blazor WebAssembly project template
As you have seen, the Demo
Blazor WebAssembly project created by the Blazor WebAssembly App project template includes quite a few files. In later chapters, we will want to start with an empty Blazor project. So, we will create our own project template that creates an empty Blazor WebAssembly project.
Creating an empty Blazor project
We need to create an empty Blazor WebAssembly project to base our new project template on. We do this as follows:
- Return to Visual Studio.
- Delete the
wwwroot\sample-data
folder. - Delete all of the components in the
Pages
folder, except for theIndex
component. - Open the
Index.razor
file. - Delete all of the markup from the
Index
component. Make sure that you do not delete the@page
directive at the top of the page. - Delete the
Shared\SurveyPrompt.razor
file. - Open the
Shared\MainLayout.razor
file. - Remove the
About
link from the top row of the layout by removing the following markup:<a href="http://blazor.net" target="_blank" class="ml-md-auto"> About </a>
- Open the
Shared\NavMenu.razor
file. - Remove the
li
elements for theCounter
andFetch data
pages. - From the Build menu, select the Build Solution option.
- From the Debug menu, select the Start Without Debugging (Ctrl+F5) option to run the
Demo
project.The
Demo
project is now empty. It only contains a blankHome
page.
Creating a project template
The Export Template Wizard is used to create custom project templates. We will use the empty project that we just created as the basis for a custom project template. We do this as follows:
- Return to Visual Studio.
- From the Project menu, select the Export Template option to open the Export Template Wizard window.
- Select Project template on the Choose Template Type dialog and click the Next button:
- Complete the Select Template Options dialog as shown in the following screenshot and click the Finish button:
After you click the Finish button, your new project template will be saved to the folder indicated in the Output location field on the Select Template Options dialog and the folder will automatically open. The files that comprise your new project template are compressed into a file called EmptyBlazorProject.zip
.
Updating a custom project template
We need to make a few updates to our custom project template before it is ready to use. First, we will declare a template parameter for the project's name, and then we will update the metadata. We do this as follows:
- Extract all of the files from the
EmptyBlazorProject.zip
file.The
EmptyBlazorProject.zip
file contains all of the files from the emptyDemo
project as well as aMyTemplate.vstemplate
file that contains all of the metadata for the project template. - Open the
Shared/NavMenu.razor
file and replace the wordDemo
with$projectname$
:<a class="navbar-brand" href="">$projectname$</a>
The
$projectname$
parameter will be replaced by the name of the project that is provided by the user when the project is created.Open the
_Imports.razor
file and replace the wordDemo
with$projectname$
:@using $projectname$ @using $projectname$.Shared
- Open the
MyTemplate.vstemplate
file. - Update the value of the
Name
element toEmpty Blazor WebAssembly App
:<Name>Empty Blazor WebAssembly App</Name>
- Add the following elements after the
Description
element:<LanguageTag>C#</LanguageTag> <ProjectTypeTag>Web</ProjectTypeTag>
- Replace the
Icon
element with the followingIcon Package
element:<Icon Package="{AAB75614-2F8F-4DA6-B0A6-763C6DBB2969}" ID="13"/>
- Change the
ReplaceParameters
attribute totrue
forNavMenu.razor ProjectItem
:<ProjectItem ReplaceParameters="true" TargetFileName="NavMenu.razor"> NavMenu.razor </ProjectItem>
- Change the
ReplaceParameters
attribute totrue
for_Imports.razor ProjectItem
:<ProjectItem ReplaceParameters="true" TargetFileName="_Imports.razor"> _Imports.razor </ProjectItem>
- Save all of the updated files.
- Update the
EmtpyBlazorProject.zip
file with the updated files. - Copy
EmtpyBlazorProject.zip
from theVisual Studio
2019\MyExportedTemplates
folder to theVisual Studio
2019\Templates\ProjectTemplates
folder.
Using a custom project template
We can use a custom project template the same way that we use any of the built-in project templates. We do this as follows:
- From the File menu, select the New, Project option.
- Enter
Blazor
in the Search for templates textbox to locate your new template: - Select the Empty Blazor WebAssembly App template and click the Next button.
- Update the project name to
Sample
and click the Create button. - From the Build menu, select the Build Solution option.
- From the Debug menu, select Start Without Debugging (Ctrl+F5).
We have created a new Sample
project by using our custom project template. The only page in the Sample
project is the Home
page.
We created an empty project by deleting some of the components and code from the Demo
project that we created in the previous section. Then, we used the Export Template Wizard to create a custom project template based on the empty project. After we updated some of the files in the custom project template, we copied them into the ProjectTemplates
folder. Finally, we used the custom project template to create the Sample
project.