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\
Related
This question is related to this one.
I’m trying to build a Vs package that’ll add context menu to the project of a specific type. Actually, I’m trying to achieve the exact thing MVC3 has. MVC3 project shows different context menus depending on which folder within the project is currently selected. For “Controllers” folder you’ll get a menu item to create Controller, and for “Views” folder you’ll get a menu item that triggers wizard for Views.
Maybe someone knows if MVC3 Vs Package sources are open? I thought they are as MVC itself is open and one can get sources easily. But I couldn’t find sources for MVC3 Vs Package…
Thank you SO in advance.
The features you describe are a bit more than simple VSPackages. Those are actually implemented in a separate binary, which can be downloaded, it is called ASP.NET MVC3 Tools Update. You can read more about it here.
The custom context menus are all defined and associated with the project type GUID. The MVC3 GUID is
{E53F8FEA-EAE0-44A6-8774-FFD645390401}
and the MVC2 project type GUID is
{F85E285D-A4E0-4152-9332-AB1D724D3325}.
So if you want to do such customizations, you do not have the MVC3 Tools binary's source for reference. I do not see it in Codeplex MVC sources anyway. You have to go to the pain of creating a ProjectType and this may help you do just that
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.
I have an ASP.NET MVC 2 web site that I've already upgraded to MVC 3. I'm not finding it very easy for new Razor views to coexist with my older ASPX views. I've found some articles, but I've failed at all of their suggestions and it seems not many people have been successful at doing this.
Has Microsoft made anything to make it easier to add Razor support to an upgraded MVC 2 app?
I haven't done this myself, however in this post by Scott Guthrie, there's this screenshot:
Given that this setting exists on the View, it suggests that you can have multiple views using different view engines. This means you should be able to run your existing code as normal, but add new views using Razor.
You can always test this out with a new project, and try adding two views - one using ASPX and one using Razor. If it all works, try diffing the test project to your existing one (specifically things like project and .config files).
You can use this tool from Telerik: https://github.com/telerik/razor-converter to convert your ASPX to CSHTML
The following steps worked for me.
Create another Temporary project based on MVC 3 Razor support by default
Compare the references, web.config (main one and one under views folder) in the Temporary project with my own project. Here I found references to System.Web.Helpers & System.Web.Webpages. Similar mentions exist in the web.config files also. Just copy those sections from the temporary project to your project.
Copy the _ViewStart.cshtml file from temporary project to your project (under Views folder)
Copy the _Layout.cshtml file from temporary project to your project (under Views\Shared folder)
Also check inside _Layout.cshtml. It may be referencing other partial views, jquery (js file) etc. Make sure they all exist in your own project.
That's it. Compile and all should be set right.
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?
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}