During one of my recent projects while talking with a colleague we were discussing the fact that in each website we created we had to perform the same tasks over and over. We decided that it would be a good idea to create a template that could be used as a starting point for all future projects, including custom error pages, global.asax, app settings, connection strings, master pages, themes and user controls. After some research I found there are several useful examples on the Microsoft site on how to do this.
The basic steps for creating a c# website project template are:
- Set up a website as normal in visual studio with all the files you want to include.
- Navigate to C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Web\CSharp\1033 and copy one of the existing templates zip files.
- Extract the zip file and copy the .vstemplate and .webproj files to the location of your own website.
- The existing .vstemplate might contain a lot of extra information but all you need to get up and running for the template data section is the following basic information
<TemplateData>
<Name>Template Website</Name>
<Description>A simple Website template</Description>
<Icon>icon.ico</Icon>
<ProjectType>Web</ProjectType>
<ProjectSubType>CSharp</ProjectSubType>
<DefaultName>Template Website</DefaultName>
</TemplateData>
- In the next section you must list all the files in your template site. For example
<ProjectItem>App_Themes/Default/SkinFile.skin</ProjectItem>
<ProjectItem>App_Themes/Default/StyleSheet.css</ProjectItem>
<ProjectItem>App_Themes/Default/images/arrow.jpeg</ProjectItem>
<ProjectItem>Controls/HeaderUserControl.ascx</ProjectItem>
<ProjectItem>Controls/HeaderUserControl.ascx.cs</ProjectItem>
<ProjectItem>MasterPage/Global.master</ProjectItem>
<ProjectItem>MasterPage/Global.master.cs</ProjectItem>
- Next zip up the files that you want to be included in the template website, including the .vstemplate and .webproject and copy them into the following location C:\Documents and Settings\YOURNAME\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual Web Developer
- Now when you open Visual Studio 2005 if you go to add a new website you'll see your new template under 'My Templates'