{"id":22011,"date":"2013-12-26T14:04:23","date_gmt":"2013-12-26T21:04:23","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/aspnet\/?p=22011"},"modified":"2013-12-26T14:04:23","modified_gmt":"2013-12-26T21:04:23","slug":"how-to-customize-the-generated-files-from-the-new-scaffolded-item-dialog","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/how-to-customize-the-generated-files-from-the-new-scaffolded-item-dialog\/","title":{"rendered":"How to customize the generated files from the New Scaffolded Item dialog"},"content":{"rendered":"<p>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 \u201cOne ASP.NET\u201d vision. We\u2019ve also released the same support for Visual Studio 2012, checkout our previous post at <a href=\"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\">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<\/a> for more info on how to get started in VS2012 with the new features.<\/p>\n<p>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\u2019d have to copy the corresponding T4 file (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb126445.aspx\">more info on T4<\/a>) 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\u2019ve simplified the end-to-end flow though by using <a href=\"http:\/\/sidewaffle.com\/\">SideWaffle<\/a>.<\/p>\n<p>In this post I\u2019ll 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 <a href=\"http:\/\/sidewaffle.com\/\">SideWaffle<\/a>. I\u2019ve got some steps at the bottom regarding the usage in case you would like to avoid installing <a href=\"http:\/\/sidewaffle.com\/\">SideWaffle<\/a>. Try the steps below to get started.<\/p>\n<h2>How to customize the generated output<\/h2>\n<p>To customize the generated content for a particular project follow these steps.<\/p>\n<ol>\n<li>Download and install <a href=\"http:\/\/sidewaffle.com\/\">SideWaffle<\/a> if you haven\u2019t already<\/li>\n<li>\n<p>Right click on your web project and chose Add New Item. Select the template shown in the image below and click OK.<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5672.SNAGHTMLf263605_690E9A48.png\"><img decoding=\"async\" title=\"SNAGHTMLf263605\" border=\"0\" alt=\"SNAGHTMLf263605\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/3108.SNAGHTMLf263605_thumb_4FA6970E.png\" width=\"644\" height=\"446\" \/><\/a><\/p>\n<\/li>\n<\/ol>\n<ul>\n<li>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.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6735.image_3D5DD04C.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/6758.image_thumb_642BE68C.png\" width=\"323\" height=\"350\" \/><\/a><\/p>\n<p>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 <strong>MVC 5 Controller \u2013 Empty<\/strong> scaffolder. Under each folder you\u2019ll find the files that the specific scaffolder will use during the scaffolding process. Since most existing scaffolders generate 1 file you\u2019ll typically find only 1 .t4 file in the directory. For scaffolders which generate more than 1 file you\u2019ll find additional files.<\/p>\n<p>Let\u2019s tweak the Empty MVC controller scaffold. Let\u2019s take a look at what\u2019s under the MvcControllerEmpty folder. That folder has a single .t4 file, Controller.cs.t4 (<em>or Controller.vb.t4 if VB<\/em>). Below is the current content of this file.<\/p>\n<pre class=\"brush: xml;\">&lt;#@ template language=\"C#\" HostSpecific=\"True\" #&gt;\n&lt;#@ output extension=\"cs\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"ControllerName\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"ControllerRootName\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"Namespace\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"AreaName\" #&gt;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Web;\nusing System.Web.Mvc;\n\nnamespace &lt;#= Namespace #&gt;\n{\n    public class &lt;#= ControllerName #&gt; : Controller\n    {\n        \/\/\n        \/\/ GET: &lt;#= (!String.IsNullOrEmpty(AreaName)) ? (\"\/\" + AreaName) : String.Empty #&gt;\/&lt;#= ControllerRootName #&gt;\/\n        public ActionResult Index()\n        {\n            return View();\n        }\n    }\n}\n<\/pre>\n<p>Let\u2019s say that you prefer to place your using statements inside of the namespace we\u2019ll modify this file to have the following content instead.<\/p>\n<pre class=\"brush: xml;\">&lt;#@ template language=\"C#\" HostSpecific=\"True\" #&gt;\n&lt;#@ output extension=\"cs\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"ControllerName\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"ControllerRootName\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"Namespace\" #&gt;\n&lt;#@ parameter type=\"System.String\" name=\"AreaName\" #&gt;\nnamespace &lt;#= Namespace #&gt;\n{\n    using System;\n    using System.Collections.Generic;\n    using System.Linq;\n    using System.Web;\n    using System.Web.Mvc;\n\n    public class &lt;#= ControllerName #&gt; : Controller\n    {\n        \/\/\n        \/\/ GET: &lt;#= (!String.IsNullOrEmpty(AreaName)) ? (\"\/\" + AreaName) : String.Empty #&gt;\/&lt;#= ControllerRootName #&gt;\/\n        public ActionResult Index()\n        {\n            return View();\n        }\n    }\n}\n<\/pre>\n<p>Now when I create a new Empty MVC controller the generated output is as follows.<\/p>\n<pre class=\"brush: xml;\">namespace WebApplication2.Controllers\n{\n    using System;\n    using System.Collections.Generic;\n    using System.Linq;\n    using System.Web;\n    using System.Web.Mvc;\n\n    public class Default3Controller : Controller\n    {\n        \/\/\n        \/\/ GET: \/Default3\/\n        public ActionResult Index()\n        {\n            return View();\n        }\n    }\n}\n<\/pre>\n<p>That\u2019s 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.<\/p>\n<h3>How to get started without SideWaffle<\/h3>\n<p>If you would like to do this without installing SideWaffle, it\u2019s still very easy. You can just copy the files from your local machine into your project. You\u2019ll find all the folders that you need to copy at <strong>C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\IDE\\Extensions\\Microsoft\\Web\\Mvc\\Scaffolding\\Templates<\/strong>. You can copy the contents of that folder into a top level folder named <strong>CodeTemplates<\/strong> 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.<\/p>\n<h3>CodeGenerators<\/h3>\n<p>The scaffolders that end with CodeGenerator are not extensible in the same way that other templates are.<\/p>\n<h3>Custom Views<\/h3>\n<p>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.<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/5344.image_6B81592D.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/63\/56\/metablogapi\/8081.image_thumb_0410F67E.png\" width=\"604\" height=\"370\" \/><\/a><\/p>\n<p>This sample is pretty basic but I\u2019m 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\u2019d 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.<\/p>\n<p>Thanks,<br \/>\nSayed Ibrahim Hashimi | <a href=\"http:\/\/msbuildbook.com\/\">http:\/\/msbuildbook.com\/<\/a> | <a href=\"https:\/\/twitter.com\/sayedihashimi\">@SayedIHashimi<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u201cOne ASP.NET\u201d vision. [&hellip;]<\/p>\n","protected":false},"author":357,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[31,7434,7461,147,7412,7436],"class_list":["post-22011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net","tag-asp-net-4-5","tag-scaffolding","tag-visual-studio","tag-visual-studio-2012","tag-visual-studio-2013"],"acf":[],"blog_post_summary":"<p>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 \u201cOne ASP.NET\u201d vision. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/357"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=22011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/22011\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=22011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=22011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=22011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}