How to add new Scaffold used in Visual Studio 2013 Scaffolding?
Asked Answered
C

1

6

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.

Cufic answered 29/10, 2013 at 17:28 Comment(2)
The way how it was done in MVC3 update 2 as shown in this video by Steve Sanderson at about 50th minute where he changes T4Scaffolding.Scaffolder attribute to T4Scaffolding.ControllerScaffolder which is a subclass of Scaffolder. Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More channel9.msdn.com/Events/MIX/MIX11/FRM13?format=smooth I believe that whatever that ControllerScaffolderAttribute class did (like registering it with MVC tools GUI, maybe implementing some interface that is picked by GUI) will work similarly with new MVC 5 Add Controller dialog box in VS 2013.Bifacial
Steve Anderson also has a series of blogs which goes into more detail for this process: blog.stevensanderson.com/2011/04/06/…Cipher
B
3

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

Bridgers answered 26/1, 2014 at 3:55 Comment(5)
Hello @Eilon. First of all, thanks for the information. But do you have any news on that? I'm looking forward to create a CreateOrEdit view.Sail
@RafaelMerlin if you want to just add a custom view scaffolder that is supported today. I just added some info to my answer on how to do that.Bridgers
Thanks for your answer @Eilon. I was able to create a new template for a CreateOrEdit view and it helped a lot. But I was aiming at the Scaffolding that generates both the controller and all the views automatically. I tried to edit my controller to remove the Detail view, it worked for the controller (that doesn't generate the Detail block anymore) but it ended up generating the detail view anyway, so, I think this is maybe a 'locked' resource. FYI: I'm using VS 2013 Express.Sail
@RafaelMerlin yeah unfortunately for that you'd need to write a custom scaffolder, which is not yet supported - but we are working on it.Bridgers
I added a new Scaffolder file (called CreateSampleView.cs.tt) inside "CodeTemplates\MvcView" in my project but it is not listed on "Scaffold Templates" list in "Add View window". What else can I do to make it work?Kildare

© 2022 - 2024 — McMap. All rights reserved.