Jason PresleyJason Presley

Custom Scaffolding

Scaffolding is a great time saver. However, as developers progress they find different patterns and techniques that they consistently use in their projects and will find themselves continually changing scaffolding the same way over and over. This can become very tedious and time consuming. Fortunately Microsoft has allowed for developers to customize scaffolding for each project. They have set up the scaffolding functionality to check for scaffolding templates in the Visual Studio project and use those templates over the global templates if any are found. To take advantage of this all you have to do is make a copy of the templates you want, modify them and include them in your project.

The first step is to dig into the Visual Studio folder and copy the templates that you want. The templates for Visual Studio 15 are located at C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates. There are numerous folders in this location. The ones that I copy are the MvcControllerWithContext folder and the MvcView folder. These contain the templates that are used when you select the MVC Controller with Views Using Entity Framework option when creating a new controller.

Once you have these folders copied create a new folder at the root of you web project titled CodeTemplates and paste the folders into this new folder. Once pasted, we can then modify the controller and views using Microsoft's T4 template code. Following is an example of some of the controller and view code that I modify.

MvcControllerWithContext > controller.cs.t4

public <#=ControllerName #>(IUnitOfWork unitOfWork)

public ActionResult Index()
return View(uow.<#=ModelTypeName #>Repository.Get());

MvcView > List.cs.t4

@Html.ActionLink("Details", "Details", new{id=item.<#=pkName #>}) |
@Html.ActionLink("Edit", "Edit", new{id=item.<#=pkName #>}) |
<a href="@Url.Action("Delete", "<#=ViewDataTypeShortName #>", new{@id=item.<#=pkName #>})" onclick="return confirm('Are you sure you want to delete item @item.<#=pkName #>');">Delete</a>
<< View All Posts