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.
Related
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
I am totally new to ASP.NET MVC 4 as I was developing into PHP (Laravel) and the way to organize things is usually something like a "public" folder which would then be separated into css, img, js, lib, and so on.... which is the way I prefer to organize my files...Trying to follow this into ASP MVC 4, and I found and read this question that is similar to what I want achieve. organizing custom javascripts in asp.net mvc 4I did it the way they said and added my custom JS files and added them in the Bundle, it compile and also run fine but the problem I have is that inside the Solution Explorer all the files that I moved are showing with an exclamation mark and also my newly created folders (directly in file explorer, not doing it through VS) are not showing by default, they are for some reason hidden, which I can see with Show All Files. I made sure that they exist inside the Bundle but then my Solution Explorer does not get refreshed. I am trying to make a structure that looks the following:
Contents
img
ui-icons.png
css
bootstrap
bootstrap.css
bootstrap.min.css
Scripts
lib
bootstrap
bootstrap.min.js
bootstrap.js
jquery
jquery.js
jquery.min.js
jquery.ui.js
jquery.ui.min.js
modernizr
modernizr.js
custom
mycustom.js
mycustom2.js
I am not crazy about dumping everything inside the same folders (Content & Scripts), including my custom files in between some official libraries. I know using directly the File Explorer is probably not the best way to go, but then what would be the official way of configuring these... and actually does my structure make sense into ASP MVC4?
EDITMy question is more related to why after creating and moving the files like jquery and others into a Scripts/lib folder, my Solution Explorer is still showin these files under Scripts but with an exclamation marks as saying file not found (of course since I moved them). Why it doesn't reflect exactly what I see in File Explorer? Apart from editing the Bundles is there anything else I'm suppose to do so that my Solution Explorer is up to date with reality??? and why are my created folders not showing in Solution Explorer, why do I have to click on Show All Files to see them? The commmand Create New Folder is not even enable as an available command, that's why I have done these folders directly in File Explorer...but why?
Frankly, you can organize your MVC project however you see fit. If there's anything close to an "official" way to organize your web files, it would be how the default Visual Studio MVC4 template organizes them:
Content (CSS files and related image files)
Images (general image files)
Scripts (JavaScript files)
If you're more familiar with another sort of organization or are part of a team that would prefer another organization, then go for it. (Though in that team scenario, make sure all the team members follow the same organization rules!).
Edit:
To alter the folder structure that the MVC template provides using the Solution Explorer, right-click on the folder in which you want to add a subfolder (this includes the project name, for project-level folders), and then select Add and then Add Folder from the pop-up menu that appears.
If you want to move around files that are already in your solution to another location within the solution, you should move them around using the Solution Explorer, not Windows Explorer, as you'll otherwise get the behavior you are experiencing, where Visual Studio does not know where you moved them. You can click and drag files around, right-click and copy and paste, etc.
If you need to add pre-existing files to your solution (say, to include a set of custom scripts), you can copy the files to the appropriate project folder using Windows Explorer. Then, in Visual Studio, highlight the project that corresponds to where you moved them, and press the Show All Files button in the Solution Explorer toolbar - this will show the files you copied in Windows Explorer, which otherwise won't be listed in Solution Explorer tab. Lastly, highlight the new files, right-click, and select the Include in Project option from the pop-up menu.
One final pointer: if you need to add a specific JavaScript library to your project, the easiest way to do so would be to use the NuGet package manager, rather than to download and add the files in Windows Explorer. This option is found in Visual Studio in the Tools menu, under Library Project Manager --> Manage NuGet Packages for Solution. Not all JavaScript libraries will be available this way, but the most popular ones are.
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
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.
In an existing VS2008 SP1 web project I'm introducing an MVC page. I was able to manually create a view and get it working just fine but I'm unable to get the "Add View" and "Add Controller" to work from the Solutions Explorer. Also, the MVC templates don't show up when I try and Add a New Item.
I know my VS2008 is setup properly because on a new project everything works fine (MVC templates show up in Web Items) and the "Add View" and "Add Controller" work fine as well. Is there a trick to get my legacy project to see these options.
I tried look around the csproj files. Nothing obvious there.
I see the template zip files in my folder ...Common7\IDE\ItemTemplates\CSharp\Web\MVC
Thanks.
My guess is that the project type is probably set wrong. I know it did change after one of the betas.
Try this: First, check in your existing files to source control, in case you accidentally mess up the project file. Open both of the *.csproj files (the one that "works" and the one that "doesn't") as text. You will have to unload the projects in the IDE in order to do this. Copy the <ProjectTypeGuids> value from the one that "works" to the one that "doesn't". Now reload the projects. See if that fixes the problem.