Introduction

Recently I published an article about creating custom project template in visual studio with additional wizard window. After reading it, one of my colleague asked me what if I want to include all commonly used NuGet packages in custom project template?

So, rather than updating the old article, I thought of writing new one on the topic.

Article Body

With increasing use of NuGet packages, It is also becomes complex to remember what all NueGet packages are being used in certain projects. Also, I can find myself using many common NuGet packages in majority of applications I create (or event sample applications I create for tutorials).

Luckily, We can also set NuGet package in our custom project template which will automatically get installed when new project is created using this project template.

I have already covered explaination about how to create custom project template here. For the purpose of reading all information in flow, I am repeating some of the information from this old article. However, if you are interested, you can visit this article first and come back again.

Creating custom project template in Visual Studio

To start with custom template, first create a new web application project. By going to File -> New Project -> ASP.Net web Application. Name the project as "CustomProjectTemplateNuget". I want to have couple of JavaScript files, CSS files and of course some NuGet packages to be part of this custom project template.

We will first add all the files and install required NuGet packages in this template project. Add required JavaScript file and CSS file in Scripts and Styles folder respectively

Note: This is optional step and can be avoided. Only you will need to update .vsTemplate XML file accordingly

Suppose, I want Asp.net sprite NuGet package to be automatically install by my project template and I dont want to install this package every time I create new project.

So, Now I add AspNetSprite NuGet package using console manager as shown here. To use console manager to install NuGet package, open package manager console (Go to View -> Other windows -> Package Manager Console) and install required packages by using install command.

The installation of NuGet package will create packages folder in root of your application. We will need to use this folder at later stage.

Creating .vsTemplate file

The next step is to create .vsTemplate file which will work as configuration for our template project. In root of the application, Right click and Add New Item. Select new .XML file and name it as ProjectTemplate.vsTemplate. Open file for editing and add below complete code in it.

The purpose of various tags inside <TemaplateData> in above XML format is evident from their name. You can change these values as per your choice (like ProjectType could be either CSharp/VisualBasic/JSharp/Web so that project template can be categorized into appropriate type).

Inside <TemplateData> tag, I have included <Icon> tag and specified name of .ico file. The Icon tag information is used to display picture of project template while creating new project. I strongly advice to read this to know more about template data.

The <WizardExtension> and <WizardData> are important tags here from NuGet package persepective. The wizard extension configures the wizard which will invoke the list of NuGet packages and help to load them. Since the WizardExtension uses in built assembly for initiate standard wizard, it is important to use this WizardExtension tag as it is (as below).

The WizardData tag specifies where to get the NuGet packages from (package repository). We can either use VSIX package repository or embed NuGet packages in template project itself.

The standard suggestion is to use VSIX repository, but for simple project template, embeding NuGet packages in template iteself is sufficient and works well. We will use NuGet package embeded in template itself.

Inside that, we have to list all NuGet packages including their id and version number (This helps in getting correct update of NuGet package). Since, we have installed Asp.net sprites NuGet package, same is listed under <packages>.

Now, only following steps are required to be followed.

Go to directory location where you have created this project and in the CustomProjectTemplateNuget directory of the application create a file named "WebApplication.webproj".

The reason: Since we are creating this Visual Studio project template for Web site project, we have mentioned this as project file name (in .vsTemplate configuration above) This file is internally used at the time of initiating new project (For web application projects, we will specify .csproj file name).

Go to root directory of application and find Packages folder along with solution file. Inside packages directory, you can find separate directories for all the NuGet packages installed. Inside every package directory, there exists .Nupkg file. Copy this .Nupkg file from every package directory and copy it into root directory of application along with .vsTemplate file

Finally, select go to root directory. which will contain files as diplayed below. select all files and, Right Click -> send to -> Compressed (zipped) folder. This will create new zip file in the same folder

Add project template file in Visual Studio

We now need to add this zip file into default template location of visual studio. Mostly (unless you have changed) VS template stored at

Read this to know how can you find default location of project template on your machine. Once the default location is identified, copy and paste the zip file here and that's it!

Using project template to create new website

To test the project template with wizard, open new Visual studio instance and go to File -> New web site. The new project name can be seen here

This will start a new web site project and install AspNetsprite NuGet package by default. This can be seen by monitoring bin folder, updated wen.config file as well as additional App_Sprites folder

Now, I have created a Custom project template which by default installes all commonly used NuGet packages whenever I create new project. Thats cool! :)

Conclusion

Visual studio can be extended very easily which help us to customize many other things. Now NuGet packages also can be added in custom templates. Although using VSIX repository is more robust and proper way to do it, For small project templates, embeding packages in template itself can suffice the need.

Hello there, I do think your site could possibly be having web browser compatibility issues. Whenever I take a look at your site in Safari, it looks fine however, when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Besides that, excellent website!

Recent Posts

Disclaimer

The opinions expressed here represent my own and not those of my past or present employers.

The concept/code provided on this site may not work as described. If you are using any code provided on this site. Then, please test it thoroughly. I shall not be responsible for any issues arising in the code.