Getting Started With New Scaffolded Item Dialog to Design Generated Files

Introduction

In the past week, Microsoft revealed the Add New Scaffolded Item dialog in Visual Studio 2013. This dialog replaced the Add View/Add Controllers ASP.NET MVC dialog that was in 2012. If you use this dialog, it'll work for all project templates like MVC, Web Forms and Web API. This is one example of how we are delivering on the "ONE ASP.NET" version. You can also get the benefit of this dialog in the Visual Studio 2012 also.

Brief

We had the great feature with Add View/Add Controller in ASP.NET MVC dialog was the ability to override the generated output of the dialog box. If you want to adjust the results of the dialog box then you need to copy the adjacent T4 file and paste it into the right spot and we'll modify the T4 file that suits our needs and then when we use the dialog box, it'll use the updated T4 file instead of the default ones. Microsoft follows the same methodology and has simplified the end-to-end flow though by using SideWaffle.

In this article, you'll learn to design the generated contents of New Scaffolded Item as explained above. So to get started proceed with the following sections.

Prerequisites

Please download and install the SideWaffle to work with ASP.NET Scaffolding T4 files.

Designing the Generated Ouput

Step 1: Open an existing web application in Visual Studio 2013.

Step 2: Just right-click on the project to add a new item and do as shown below:

Adding ASP.NET Scaffolding T4 Files

Step 3: Click on "Add" and now you can design the T4 files under the CodeTemplates folder.

When you click on Add, it'll drop all the .t4 files you need into a top level folder in the project named CodeTemplates. This will depend on the language of your project. Each folder under this folder represents an individual scaffolder that is shipped. You can see that in the following screenshot :

CodeTemplates in VS 2013

In this folder the names are very straightforward, like if we select the ApiControllerEmpty, it corresponds to the Empty Web API 2 Controller. So the following code snippet represents the ApiControllerEmpty folder:

<#@ 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" #>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

 

namespace <#= Namespace #>

{

    public class <#= ControllerName #> : ApiController

    {

    }

}

 

Step 4: Suppose we want to use the using statements inside the namespace, then simply use the using statements inside the namespace in here as shown below:

<#@ 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" #>

 

namespace <#= Namespace #>

{

       using System;

       using System.Collections.Generic;

       using System.Linq;

       using System.Net;

       using System.Net.Http;

       using System.Web.Http;

 

    public class <#= ControllerName #> : ApiController

    {

    }

}

 

Step 5: Create a Web API 2 Empty Controller.

Creating Web API 2 Empty Controller

Step 6: It'll look like as follows:

namespace MvcStudentApp.Controllers

{

       using System;

       using System.Collections.Generic;

       using System.Linq;

       using System.Net;

       using System.Net.Http;

       using System.Web.Http;

 

    public class MyApiController ApiController

    {

    }

}

 

That's all we need to do in here.

Summary

This article has introduced you to customizing your generated output with the ASP.NET Scaffolding T4 files using the SideWaffle in Visual Studio 2013. Thanks for reading.