Where to put custom view templates in MVC 3? - asp.net-mvc

I want to create a replacement T4 template that can be called from Visual Studio's "add view". Where should I put the file? I'm not sure I should put it in
C:\Program Files\Microsoft Visual Studio 10.0\
Common7\IDE\ItemTemplates\VisualBasic\Web\
MVC 3\CodeTemplates\AddView\VBHTML
I suspect it will get wiped during repair etc if I put it there.

You can put your custom T4 template under your project like this:
{YourProjectFolder}\CodeTemplates\AddView
Of course this way it will only be enabled for this specific project, while putting them in the location you mention in your question will enable it for all your projects system wide.
Here you can find an interesting article going further into the details:
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
There's even already a nuget package who does all the plumbing for you:
Add the ASP.NET MVC 3 Code Templates to your application with Nuget

create the following structure in the ROOT of your Project (Not solution):
CodeTemplates\AddView\VBHTML (in your case, or CSHTML for C# razor views) and copy the templates from that folder into the newly created VBHTML folder.
Also in set the Tool property in the files' Property to empty, otherwise it will ask to build the *.tt file every time you save. Also consider using Tangible T4 Editor extension for Visual Studio to edit the templates. It's available on http://visualstudiogallery.msdn.microsoft.com/

There are two possible locations. Either the one you showed which will be global for all projects on the system or in ~\CodeTemplates\AddView\VBHTML\MyTemplate.tt if you want this template to be available only for a given project. Here's a blog post about it.

Related

Excluding shared editor templates from project in Visual Studio doesn't work

I want to run my ASP.net MVC project without using a particular shared editor template that has errors
eg.
~\Views\Shared\EditorTemplates\String.cshtml
But even when I right click and exclude it from the project, it is used when the project is run and it errors out. What should I do to tell Visual Studio to ignore this template?
I'm using Visual Studio 2012 Professional.
If you want to keep the file, but not have ASP.Net MVC use it, rename the file. Call it -String.cshtml.
DisplayTemplates and EditorTemplates are looked up in their respective directories, matching filenames to the type they are used on. When you exclude them from the project they are physically still present and found, therefore still used.

MVC Scaffolding with Custom T4 Templates in VS 2012 Express

Just getting started with customizing the generated MVC Controller / Views.
Following Steven Sanderson's post:
http://blog.stevensanderson.com/2011/04/06/mvcscaffolding-overriding-the-t4-templates/
I used the Package Manager Console to generate 3 custom templates in my VS 2012 MVC4 project
PM>Scaffold CustomTemplate View Create
PM>Scaffold CustomTemplate View Edit
PM>Scaffold CustomTemplate View _CreateOrEdit
which creates a the files in:
CodeTemplates\Scaffolders\MvcScaffolding.RazorView
_CreateOrEdit.cs.t4 Create.cs.t4 Edit.cs.t4
But, when I "Add Controller" and choose the template
Mvc Controller with read/write actions and views using Entity Framework
The new templates are NOT being used ???
I was able to modify the create.tt edit.tt templates with notepad in
the VS 2012 folder and then generate with the changes in my project.
C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\VWDExpress\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML
Anyone know what I'm doing wrong ?
Thanks, LA Guy
Try creating a CodeTemplates\AddView\CSHTML folder inside your solution and put your templates there (or CodeTemplates\AddController for a controller template). It worked for me in VS2012. These can be checked into source control and everyone can share them. The ones in your VS install directory are a bit harder to share.

How to write an MVC3 Project Template that will offer option Razor or .aspx views

I have written two separate project templates to create MVC3 projects (both based on the standard Microsoft template, but with additional controllers/views of my own) - one with ASPX views, and the other with Razor views.
But, rather than have two separate templates, I would rather mimic the way that the Microsoft MVC3 template works, which offers both view engine options, selected in a second dialog. I know that this is done with a Wizard, but I can't find the Wizard in the MVC source code (which I've downloaded). Does anybody know where to find the Wizard, or have other helpful suggestions? Thanks.
The custom MVC New Project dialog exists in Microsoft.VisualStudio.Web.Mvc.3.0.dll. We do not ship the source code for that assembly.
I used the Export Template Wizard to create an ASP.NET MVC3 Project Template of my own. This worked OK, but did not offer the same options that Microsoft ships with their Visual Studio MVC assembly. I also ran into an issue where the strongly-typed views were not being updated with the correct namespace. They were keeping the namespace of my original project, while the Controllers and Model was correct.
We would love to have the source code made available for MVC 3 Project Templates!
Here is the Wizard I used. It definitely comes in handy. http://visualstudiogallery.msdn.microsoft.com/57320b20-34a2-42e4-b97e-e615c71aca24/
Phil Haack has documented how to create a custom MVC3 project template that will appear inside that wizard (as opposed to a normal project template that won't)
haacked.com/archive/2011/06/06/creating-a-custom-asp-net-mvc-project-template.aspx
If you follow that process you'll only end up with razor as the possible view engine. To get other view engines, you need to expand the reg files to list spark etc. Details on doing that are provided in this stack overflow answer:
Add custom viewengine to New project dialog?

Adding asp.net mvc to webforms application - how do I get MVC menu options in VS2010?

I have an ASP.NET WebForms application that I'm adding some ASP.NET MVC to.
I have it working, but how can I get Visual Studio to recognise that it's now an MVC project and give me the context menu options to add a view, etc. inside a controller action method?
Open the .csproj file with your favorite text editor (not Visual Studio) and add the following GUID:
<ProjectTypeGuids>{F85E285D-A4E0-4152-9332-AB1D724D3325};{fae04ec0-301f-11d3-bf4b-00c04f79efbc};{349c5851-65df-11da-9384-00065b846f21}</ProjectTypeGuids>
Notice the {F85E285D-A4E0-4152-9332-AB1D724D3325} GUID. This is what indicates that it is an ASP.NET MVC project.
Once this GUID added to the ProjectTypeGuids node you will get the context menus.
I don't know the specifics, but the easiest way is to create a new MVC project, and then use BeyondCompare (or your comparison tool of choice) on the two project files and see what is missing from your project. Then move those lines over and it should work.
The project file is just an XML file behind the scenes, so doing a text compare is relatively easy.
In VS11 you have to enter this GUID into the ProjectTypeGuids:
{E53F8FEA-EAE0-44A6-8774-FFD645390401}

Conditionally Displaying Item Template in Visual Studio

ASP.NET MVC has a bunch of custom item templates to create controllers, views, etc. Those item templates appear to only show up in the Add New Item dialog box when you are inside an ASP.NET MVC project (or more specifically when the MVC project type guid is in the .csproj file). How do they manage to do this? I've looked through the .vstemplate files and there doesn't appear to be anything related to the project type guid.
Thanks,
Nathan
I believe I figured it out. It looks like the TemplateGroupID node in the .vstemplate file specifies the name of the VS Project (as read from the registry) to show this template for.
The logic for these is in the T4 templates.
The Add Controller and Add View dialogs both perform code generation that use T4 templates behind the scenes. These templates can be modified to customize the generated code from these tools. You can find the templates at the following location:
[Visual Studio Install Directory]\Common7\IDE\ItemTemplates[CSharp | VisualBasic]\Web\MVC\CodeTemplates\

Resources