ASP.NET MVC - "Add View" Outside of the Views Folder - asp.net-mvc

Are there any changes I can make to Visual Studio to add the "Add View" dialog option to a folder other than Views? I have my website split up somewhat differently and I'd still like access to the T4 templates provided by "Add View".
To clarify:
I am not trying to change the default View Engine or do anything in code pertaining to how my Views work, I simply would like to know if there is any way to have the "Add View" context menu item display on Solution Explorer items other than just the "Views" folder. My application has an "Areas" folder which contains a "Views" sub-folder for each Area and I would like to have the "Add View" menu item available when I right click on Areas > AREANAME > Views and select "Add".

It seems this behavior is hardcoded in Microsoft Visual Studio 9.0\Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll file.
I think you could:
try reverse engineering this library;
ask MVC team members for its source code.

Related

Add View in Visual Studio 2015 RC

Just wondering where they may have moved the tooling to right click on an action method and add a view. I get no menu option.
Right now (ASP.Net 5/Core 1.0 RC1), that tooling does not exist.
There are plenty of places where the tooling is not there yet. For example, in the 'Web API' project template you cannot use scaffolding or add authentication during project creation via Visual Studio.
The 'web application' project template is the most complete but still has holes.
What I normally do is just copy one of the other views (About.cshtml for example) and edit the places I need to edit, you'll get to the same result.
Thanks

Template is not visible in backoffice, when created in VS

When I create a new template (.cshtml) in the Views folder of my Visual Studio solution it is not showing up in the backoffice.
If I create a template from the backoffice a file is created in the Views folder that I then have to add to VS as a exciting file - why don't the backoffice show the template if I create one from Visual Studio? I've tried rebuilding the project.
You can do it in 2 ways:
Create your views in umbraco backend. Then go to visual studio and click the "Show All Files" button in solution explore. Find your new views in the Views folder. Right click on each file and choose "Add file to project".
Create your views in visual studio as normal. Then go to umbraco backend and create your Templates, and give them the same name as the view in visual studio. (If your template name in umbraco is "Text Page", -your file name should be "TextPage")
As ProNation says, all templates needs a db reference.
Views/templates work differently to some of the other files like scripts and CSS because they require a reference in the database to allow you to associate them with your doctypes.

create area in MVC project

In an MVC4 web application, what is the best way to create an Area? Can it be automated or is it a manual process of creating the controller, View folder etc?
Right click on the ASP.NET MVC project in the solution explorer and select Add Area... in the contextual menu. Then type the name of the area and proceed:

"Nest Related Files" icon not available in asp.net mvc app

In Asp.Net I create ViewModels that are the models for a particular view; one viewmodel per view. Because it's a one-to-one relationship, I like keeping the ViewModel as a nested file to the view. It's very tidy this way.
In Asp.Net MVC files will appear nested most of the time. However, sometimes they become unnested. I can remedy this by cutting-and-pasting-and-renaming the files, which renests them. However, it sure would be nice to have the 'nest related files' appear in the solution explorer in Visual Studio for an MVC project.
Any ideas why that icon ("nest related files") is missing from Visual Studio? and how might I get it back?
Thanks!
You can do this with the VSCommands Extension. Take a look in the studio gallery.
The alternative is either as you have been doing - copying and pasting files, or by editing the project file directly.
Found it - check on the VSCommands feature page - it's listed as Group/Ungroup Items
http://vscommands.com/features/
To group items, in solution explorer highlight both items you want to group (parent & child), r-click the child and choose "Group Items".
To ungroup items, in solution explorer highlight the child you want to ungroup, r-click and choose "Ungroup Items".

How do I create my own Scaffold Template in ASP.NET MVC 3?

ASP.NET MVC provides the ability to select a 'Scaffold template' upon which a newly-created view will be based (Add View > Create a strongly-typed view > Scaffold template).
Is it possible to create your own Scaffold Template? And if so, how?
ASP.NET MVC uses T4 templates. Here's an overview.
Here are the steps:
In the Package Manager Console type: install-package mvc3codetemplatescsharp
Accept all the warnings
The CodeTemplates folder will be added to your project containing the templates
From here you could either modify the existing templates or add new one.
Or if you want to modify those globally you could to this in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\ folder.
You can use T4 without nuget of course: Place a folder in the root of the application website (the project containing the views). The directory structure is important so it should be
\CodeTemplates\AddView\AspxCsharp\MyTemplate.tt
You can copy the contents from one of the existing templates located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\
Next, clear the property on the TT file named "Custom Tool". This should be blank.
Then right-click on any Controller Action and say "Add View" or since the controllers are in a separate project in our case, right click on the View folder and click "Add View".
From the dropdown Click "Create a strongly typed View" and then enter the type to use under "View Data Class:"
Finally, in the "View Content" dropdown, select "MyTempate". This should show up if you've entered the folders correctly.
In Visual Studio 2012 with MVC 4, the easy way (install Nuget package) gets you an incomplete setup, because the Nuget package is woefully out of date (last updated in 2011 - perhaps the day it was created).
You have to use the equivalent of Francis Shanahan's answer, but instead the path to copy things from is (64-bit):
C:\Program Files (x86)\Microsoft Visual Studio\11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates
You:
Add a Reference to Microsoft.Web.Infrastructure, which you can find in Assemblies > Extensions.
Copy this CodeTemplates folder (including the folder itself) to the root of your Project and Include it in the Project
Ignore the compiler errors for now (like can't find MvcTextTemplateHost)
Go through the Properties of each of the added files and delete the text in the "Custom Tool" property of each. When you do the expand arrow next to each file will disappear (because the file will no longer be generated in-place)
Save All and build - compiler errors gone
If the compiler errors don't go away - especially if you're seeing an error in a generated .cs file Visual Studio can't find, and a .tt file - close Visual Studio, wipe your temp folder, and reopen the solution. In one case I went so far as restarting before the issue cleared up. It was caused by a generated .cs file from a .tt template that Visual Studio was still trying to automatically generate code for.
In addition, the names of the .tt files are a little bit confusing - here's how they map:
GUI:
Empty MVC controller
MVC controller with read/write actions and views, using Entity Framework
MVC controller with empty read/write actions
Empty API controller
API controller with read/write actions, using Entity Framework
API controller with empty read/write actions
CodeTemplates\AddController's files map respectively:
Controller.tt
ControllerWithContext.tt
Controller.tt
ApiController.tt
ApiControllerWithContext.tt
ApiController.tt
There's an if statement in Controller.tt and ApiController.tt that handles the with/without read/write actions functionality.
For the views, naming is intuitive except that List.tt creates Index.cshtml, and Empty.tt is used for any View besides Create/Delete/Details/Edit/Index.
This question covers what properties you can use in the .tt templates.
.tt templates are Microsoft T4 templates. T4 Template Syntax.
Or, for Visual Web Developer Express on a 32-bit system, another location for these files is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\VWDExpress\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates
This MSDN article discusses Scaffolding and Page Templates:
http://msdn.microsoft.com/en-us/library/cc488540.aspx
Which, in turn, links to the following article for modifying the templates:
http://msdn.microsoft.com/en-us/library/cc488537.aspx

Resources