How to customize the generated files from the New Scaffolded Item dialog

Sayed Ibrahim Hashimi

In Visual Studio 2013 we introduced the Add New Scaffolded Item dialog. This dialog replaced the Add View/Add Controllers ASP.NET MVC dialog which was had in 2012. This new dialog works for all ASP.NET projects (MVC, Web Forms and Web API). This is one example of how we are delivering on the “One ASP.NET” vision. We’ve also released the same support for Visual Studio 2012, checkout our previous post at http://blogs.msdn.com/b/webdev/archive/2013/11/18/announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx for more info on how to get started in VS2012 with the new features.

One of the really great features that we had with the Add View/Add Controller ASP.NET MVC dialog was the ability to override the generated output of the dialog box. The method to do this was pretty simple. If you wanted to tweak the result of the dialog box you’d have to copy the corresponding T4 file (more info on T4) into the right spot in your project, modify it to suit your needs and after that when you invoke the dialog box the updated t4 file(s) will be used instead of the default ones. We follow this same pattern. We’ve simplified the end-to-end flow though by using SideWaffle.

In this post I’ll first describe how you can customize the generated contents of the New Scaffoldded Item dialog box as well as explain how that works. To simplify the end-to-end flow we will be using using SideWaffle. I’ve got some steps at the bottom regarding the usage in case you would like to avoid installing SideWaffle. Try the steps below to get started.

How to customize the generated output

To customize the generated content for a particular project follow these steps.

  1. Download and install SideWaffle if you haven’t already
  2. Right click on your web project and chose Add New Item. Select the template shown in the image below and click OK.

  3. SNAGHTMLf263605

  • Customize the .t4 files found under the CodeTemplates folder in your project After you invoke the ASP.NET Scaffolding T4 files item template it will drop all the .t4 files you need into a top level folder in your project named CodeTemplates. Based on your project language (C#/VB) the correct files are added to the project. Each folder under this folder represents an individual scaffolder that we ship. In the image below you can see these folder names.

image

You can connect the folder to the scaffolder by the name of the folder. The names are pretty straight forward, for example the MvcControllerEmpty folder corresponds to the MVC 5 Controller – Empty scaffolder. Under each folder you’ll find the files that the specific scaffolder will use during the scaffolding process. Since most existing scaffolders generate 1 file you’ll typically find only 1 .t4 file in the directory. For scaffolders which generate more than 1 file you’ll find additional files.

Let’s tweak the Empty MVC controller scaffold. Let’s take a look at what’s under the MvcControllerEmpty folder. That folder has a single .t4 file, Controller.cs.t4 (or Controller.vb.t4 if VB). Below is the current content of this file.

<#@ template language="C#" HostSpecific="True" #>
<#@ output extension="cs" #>
<#@ parameter type="System.String" name="ControllerName" #>
<#@ parameter type="System.String" name="ControllerRootName" #>
<#@ parameter type="System.String" name="Namespace" #>
<#@ parameter type="System.String" name="AreaName" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace <#= Namespace #>
{
    public class <#= ControllerName #> : Controller
    {
        //
        // GET: <#= (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#= ControllerRootName #>/
        public ActionResult Index()
        {
            return View();
        }
    }
}

Let’s say that you prefer to place your using statements inside of the namespace we’ll modify this file to have the following content instead.

<#@ template language="C#" HostSpecific="True" #>
<#@ output extension="cs" #>
<#@ parameter type="System.String" name="ControllerName" #>
<#@ parameter type="System.String" name="ControllerRootName" #>
<#@ parameter type="System.String" name="Namespace" #>
<#@ parameter type="System.String" name="AreaName" #>
namespace <#= Namespace #>
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    public class <#= ControllerName #> : Controller
    {
        //
        // GET: <#= (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#= ControllerRootName #>/
        public ActionResult Index()
        {
            return View();
        }
    }
}

Now when I create a new Empty MVC controller the generated output is as follows.

namespace WebApplication2.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    public class Default3Controller : Controller
    {
        //
        // GET: /Default3/
        public ActionResult Index()
        {
            return View();
        }
    }
}

That’s all there is to it. You can check in the files and then when your team members get latest they will have the same behavior.

How to get started without SideWaffle

If you would like to do this without installing SideWaffle, it’s still very easy. You can just copy the files from your local machine into your project. You’ll find all the folders that you need to copy at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates. You can copy the contents of that folder into a top level folder named CodeTemplates into your project as shown above. FYI if you do this both C# and VB files will be added to your project. You can simply get rid of the files that do not correspond to the language that your project is using.

CodeGenerators

The scaffolders that end with CodeGenerator are not extensible in the same way that other templates are.

Custom Views

You can create your own view Templates by creating a new .t4 file in the MvcView folder. For example in the MvcView folder I copied the Create.cs.t4 file and renamed it to Sayed.cs.t4 file and customized the contents. After that when invoking Add View the resulting Add View dialog contains my custom view template. You can see this in the image below.

image

This sample is pretty basic but I’m sure that you can imagine much more sophisticated tweaks for your particular needs. Please let us know what you think about these features in the comments below. I personally really love the model where we generate some content by default but you can tweak that by checking in some artifacts into your project. I’d like to see where we can apply this same thinking in other areas across the product. Keep an eye out in the future for similar efforts.

Thanks,
Sayed Ibrahim Hashimi | http://msbuildbook.com/ | @SayedIHashimi

0 comments

Discussion is closed.

Feedback usabilla icon