Creating custom templates in Visual Studio

Posted on January 30, 2011 by Frederik Vig in ASP.NET, EPiServer, Visual Studio

One abit hidden featured in Visual Studio is the custom item templates and projects feature. This allows you to easily export a project or item as a template, which you then can easily reuse across different projects, and even share between your collegues and team mates. This makes it very easy to create templates for your projects that follow the best practices you and your team mates have agreed on.

At Geta we have a set of guidelines and best practices that we follow in our projects. For instance we turn off ViewState and EventWireUp. The reason for turning off EventWireup is because of performance. When it’s turned on (default), ASP.NET uses reflection to look for special methods like Page_Load. What we do instead is override the OnLoad method. We also inherit from base classes and use master pages. Our user controls and web forms usually look something like this:

<%@ PageLanguage="C#" MasterPageFile="~/Templates/MasterPages/Site.Master" AutoEventWireup="false" EnableViewState="false" CodeBehind="WebForm1.aspx.cs" Inherits="Web.WebForm1" %>
 
<asp:Content runat="server" ContentPlaceHolderID="ContentRegion">
 
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PageTypes;
using Web.Templates.Base;
 
namespace Web
{
    public partial class WebForm1 : TemplatePageBase<BasePageData>
    {
 
    }
}
<%@ Control Language="C#" AutoEventWireup="false" EnableViewState="false" CodeBehind="WebUserControl1.ascx.cs" Inherits="Web.WebUserControl1" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PageTypes;
using Web.Templates.Base;
 
namespace Web
{
    public partial class WebUserControl1 : UserControlBase<BasePageData>
    {
 
    }
}

To convert this item to a item template go to File -> Export Template. Choose Item template and select the web form and user control, click next and give it a name and description.

Export Template Wizard in Visual Studio

When we now create a new item we can choose from our newly created item templates.

New item dialog in Visual Studio

Related Posts: