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

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

Rate This
  • Comments 13

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.
    1. SNAGHTMLf263605
  3. 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

Leave a Comment
  • Please add 4 and 2 and type the answer here:
  • Post
  • Thanks..

  • Good stuff..

  • Very interesting and useful

  • Hi,

    Thanks for your helpful post, But this is not work for me! I ask the question in:

    stackoverflow.com/.../sidewaffle-template-does-not-work

  • @Seyed, I have replied there.

  • I used MVC Scaffolding before VS 2013. It used dynamic transform for Model. What has changed in VS 2013 for scaffolding and do we get full intellisense for T4 templates? Also is it possible to use the scaffolding for other type of projects? In enterprise scenario, models, data access, business layer all are in different projects also there may be WCF in between. How can we use scaffolding for the same?

  • I used MVC Scaffolding before VS 2013. It used dynamic transform for Model. What has changed in VS 2013 for scaffolding and do we get full intellisense for T4 templates? Also is it possible to use the scaffolding for other type of projects? In enterprise scenario, models, data access, business layer all are in different projects also there may be WCF in between. How can we use scaffolding for the same?

  • Still it does not work for me. I installed "ASP.NET and Web Tools 2013.1 for Visual Studio 2012" as described in "blogs.msdn.com/.../announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx". But steel it does not work. My question is: Should I add Mvc Empty controller to project in specific way? I mean currently I add Empty controller as follow:

    Right Click on Controllers Folder and select Add->New Item and in Add Controller Box in Scaffold Options select Mvc Empty Controller for template field.

  • @Seyed, it looks like you are using VS2012. What type of project do you have? Is it an MVC project? If so what version?

  • Thanks, Yes I use VS2012, I create " ASP.NET MVC 4 Web Application". Also when i create MVC3 application, it does not work.

  • I like the helpful info you provide in your articles. I’ll bookmark your blog and check again here frequently. I’m quite certain I’ll learn plenty of new stuff right here! Good luck for the next!

  • How to make the custom template(for ex Sayed.cs.t4) run when generating a new controller?

  • Hi Sayed , I followed your instruction step by step, and still i don't see my custom template in add view dialog , I use VS 2013 update 3 . Do you have any idea what's wrong ?

Page 1 of 1 (13 items)