10

I'm using ASP.NET MVC5 and VS2013

I've tried to copy CodeTemplates folder from

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\VWDExpress\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates

with subfolders "AddController", "AddView" and T4 templates inside them, then I modified some templates, but nothing happened when I tried to add View nor Controller..

Roman
  • 179
  • 1
  • 1
  • 10

5 Answers5

19

First, it looks like you have Visual Studio 2013 and 2012 both installed on your computer. I tried looking up the path you provided, I couldn't find it. On your path it looks like you're trying to use MVC4 templates. Here is my path:

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

Below is how I customized my scaffold template for the BaseController:

  1. Create folder called "CodeTemplates" directly in the project folder.

  2. Go to below path, find MvcControllerWithActions folder copy all to folder "CodeTemplates"

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\
    Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates
    
  3. Go to CodeTemplates -> MvcControllerWithActions in your project.

  4. There are two files, Controller.cs.t4 and Controller.vb.t4 in MvcControllerWithActions, if you used C#, you can delete Controller.vb.t4.

  5. Open the Controller.cs.t4 file, modify the Controller name to BaseController, like below:

    public class <#= ControllerName #> : BaseController
    {
    }
    
  6. Now, when you try to create MVC5 Controller using "add new scaffold item", it'll use the template you customized.

Hope it helps.

Klors
  • 2,665
  • 2
  • 25
  • 42
Lin
  • 15,078
  • 4
  • 47
  • 49
  • 1
    Thanks, it works well! I my case I'm using VSExpress, so actual path is: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\VWDExpressExtensions\Microsoft\Web\Mvc\Scaffolding\Templates – Roman Nov 18 '13 at 08:04
  • Funny, mine are located at: `C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\1033` – Eric Bishard Feb 17 '14 at 02:47
  • `1033`? WHy doesn't my folder have `Scaffolding/Templates`? Weird – Eric Bishard Feb 17 '14 at 02:48
  • Instead of modifying the template, I want to add new template so that I have option to select either default or my customized template depending on the requirement. Is this possible ? I tried to do it my copying the default template in my solution and renaming it. But when I add controller it doesn't show my template there. – Rajesh Naik Oct 11 '16 at 05:12
3

MVC4 and MVC5 templates are located in different locations. While MVC4 templates are located in the folder you've given, MVC5 templates are located in this folder:

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

What you need to do for customizing is to create a folder named CodeTemplates your project folder, and copy the templates you want to customize to the CodeTemplates folder.

You'll need to copy MvcView and MvcControllerWithContext folders in order to use with MVC 5 Controller with view, using EntityFramework command.

CAUTION

  • Both directories must have the same folder structure. What I mean is, do not copy the Create.cs.t4 in the MvcView folder directly into the CodeTemplates folder. Just copy the entire MvcView folder into CodeTemplates folder.

  • Do not delete include files such as Imports.include.t4 and ModelMetadataFunctions.cs.include.t4 because other files need them to be there.

  • If you're using C#, feel free to delete files with .vb.t4 extension and vice versa.

Sinan ILYAS
  • 1,212
  • 1
  • 12
  • 10
2

The MVC5 T4 templates are located in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\VWDExpressExtensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView

2

In VS 2015 templates found in:

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

drneel
  • 2,887
  • 5
  • 30
  • 48
vipasane
  • 355
  • 2
  • 10
1

For Visual Studio 2017 MVC5 T4 templates are located in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView

Chetan Sharma
  • 161
  • 11