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 a controller and view code that I modified.


public (IUnitOfWork unitOfWork){

public ActionResult Index(){
     return View(uow.Repository.Get());


@Html.ActionLink("Details", "Details", new{}) |
@Html.ActionLink("Edit", "Edit", new{}) |
<a href="@Url.Action("Delete", new { })" onclick="return confirm('Are you sure you want to delete item');" class="btn btn-danger">Delete</a>

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *