Question

Is there a way to add a new Scaffold or override the Scaffolding functionality used in the new Visual Studio 2013 Scaffolding?

The only documentation I can find on the internet is how to override the T4 templates using the CodeTemplates folder. I want to add a new Scaffold to the list when Add... > New Scaffold Item... It looks like in Visual Studio 2013 this has been rolled into a library (Microsoft.AspNet.Scaffolding.Mvc.5.0.dll) and registered in the GAC or as a VS Extension.

http://weblogs.asp.net/imranbaloch/archive/2013/09/15/customizing-the-asp-net-mvc-5-web-api-2-scaffolding-templates.aspx?CommentPosted=true#commentmessage

I want to override the mechanism that calls the T4 templates so I can create different files and enhanced the functionality. My specific need is that I want to scaffold the old CreateOrEdit.cshtml and a new DetailsOrDelete.chtml partial views. I would also like to Scaffold a tool that takes a resource file and generates the localized resource file in another language.

Was it helpful?

Solution

In Visual Studio 2013 RTM there is no support for adding completely custom scaffolders. You can only do the following customizations:

  1. You can edit or override the existing T4 templates for controllers, areas, views, etc.
  2. You can add custom view scaffolders that will show up in the existing MVC View scaffolder

The Visual Studio team is working on re-enabling custom scaffolder support in a future update of Visual Studio.

To add a custom view scaffolder today:

  1. Create any ASP.NET project in VS2013
  2. Add a folder called CodeTemplates
  3. Create a sub-folder in there called either MvcView or MvcViewWithoutModel depending on whether your custom view template is a strongly-typed view
  4. Create a file there in the form of <templatename>.<lang>.t4 where the <templatename> is whatever you want to show up in the MVC View scaffolder's drop down list and <lang> is either cs or vb.

To get started you can copy any existing scaffolder from VS2013's default list and customize it. You can get the built-in scaffolders from here:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcViewWithoutModel

If you use the same name as an existing scaffolder then it will override the built-in scaffolder, even when called from another scaffolder. For example, the MVC controller scaffolder will use an overridden view scaffolder that is in your project.

4/22/2014 Update

Now that previews of Visual Studio 2013 Update 2 are available, it is possible to write completely custom scaffolders.

Check out this blog post that walks through how to write a custom scaffolder: Creating a Custom Scaffolder for Visual Studio

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top