MVC Scaffolding with Custom T4 Templates in VS 2012 Express - asp.net-mvc

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.

Related

How Customize Scaffolding MVC Controller in Visual Studio 2019

I want to have a Template to build my MVC project controllers.
These controllers are CRUD type. Is it possible to use custom Scaffolding?
Can I Design and Run a Template for Scaffolding myself?
After searching and searching the internet, I found and searched for Scaffolding system files, but there was no VS2019 installation folder.
How can I do this?

How create your own ASP.NET MVC scaffolding template for views?

How create and use your own ASP.NET MVC scaffolding template?
In "add Controller" wizard there is an "template" option with Microsoft templates. I would like to add new ones. A customized one. is that possible?
edit
In Visual Studio 2015 there is no template option anymore.
You can modify the T4 templates used by Visual Studio. This is a good article on how to do it:
https://www.credera.com/blog/technology-insights/microsoft-solutions/create-custom-scaffold-templates-asp-net-mvc/
You copy the default templates into your project "CodeTemplates" folder. You can then alter the templates without affecting the defaults.

how to add asp.net mvc templates for monodevelop

I'm on archlinux using mono 4.0.4 and monodevelop 5.9.
By following the Getting started guide I noticed there are some scaffolding options missing, I see some nuget packages for scaffolding but installing them didn't do much.
SO has these two questions :
How to get the MVC 3 or 4 template in MonoDevelop?
Developing ASP.NET MVC using MonoDevelop on MAC
The latter is for Mac and both of them are outdated As I'm talking about asp.net mvc 5 here.
searching didn't reveal anything apart from the Nuget packages I added and I can't see any addins in the add-in manager for this(prefer not to use beta or alpha addins).
So what to do to get these ?
P.S:
I know I can get them with yoeman but I like to use monodevelop, if yoeman can be integrated with monodevelop So I can see options like "model with view and controller" scaffolding(that is a CRUD ready application) then I'll do it.
I am assuming you are talking about the templates that are available in an ASP.NET MVC application when you select Add View or Add Controller. If this is not the case then please update your question with more information.
The templates listed when adding a new view or controller to an ASP.NET MVC project are taken from t4 template files (.tt) in the following folders:
AddIns\AspNet\CodeTemplates\CSharp\AddController
AddIns\AspNet\CodeTemplates\CSharp\AddView\Razor
You can add extra .tt files into this folder and they will be displayed in the Add View and Add Controller dialog.
Out of the box MonoDevelop/Xamarin Studio only has a few t4 templates available so you would need to either write your own or find some from elsewhere. SharpDevelop has more T4 templates or MVC which could probably be used but may require some modification.

No ASP.NET MVC 5 Web Application Template on VS 2012?

I'm using Visual Studio 2012 and have just installed "Microsoft ASP.NET and Web Frameworks 2013.1 - Visual Studio".
As expected, it added the option to create an "ASP.NET MVC 5 Empty Project". The thing is, I see no way of jump-starting an already functional "base application" as I had when I was using MVC 4 (e.g. "Asp.NET MVC 4 Web Application"). I only see an empty template.
I've seen answers such as:
How can I add the MVC 5 project template to VS 2012?
Create and Run MVC 5 Project in VS 2012
Direct download link to ASP.NET MVC 5 for VS 2012
And found this article on the matter.
This is obviously not a show-stopper as I can always create the application from a scratch, but I still would like to know if it is possible to achieve that on VS 2012. Did I miss a step, or do I need VS 2013 for that? A fully functional Web application template (with authentication etc.) would be nice to dabble around MVC 5's new features or for prototyping.
After creating a new project using ASP.NET MVC 5 Empty Project, you can right click on the project, then click Add.. > New Scaffolded Item..., select MVC on the left, then MVC 5 Dependencies and choose Full dependencies from the prompt.
This will add a default layout (Views\Shared\_Layout.cshtml), Content folder, Scripts folder with bootstrap, jquery, modernizr, etc, and other things the Web Application template creates.
If you get the error "CS0103: The name 'Styles' does not exist in the current context" just add <add namespace="System.Web.Optimization"/> under <namespaces> in Views/web.config
The answer from Sean Lynch is nice but not really complete.
However there is this nice guy that took the time to prepare the template just like in Visual Studio 2013. You will get everything down to the bootstrap template and pre-configured database table for authentication.
All you need to do is change the DefaultConnection in Web.Config to you SQL Server and voila all the relevant authentication table will be created for you.
Archive of the original blog (by web.archive.org)
Update
The original blog is no longer online, but thankfully his template is still online. You can download the MVC5 Template for Visual Studio 2012 Here:
Direct link to MVC5 Template For Visual Studio 2012
And here is the excerpt from the original blog:
Installing the template is very simple, there are just two steps:
Copy it into the following folder of your computer (creating the directory structure if necessary):
%USERPROFILE%\Documents\Visual Studio 2012\Templates\ProjectTemplates\Visual C#\Web
Once you’ve done this, simply restart Visual Studio and you will have this template available in the dialog box to create a new project under “Visual C# > Web”.
Now you can create applications with a little more shape than the mere empty structure provided by default and enjoy the benefits of adopting Bootstrap and its responsive design, a full membership system and user authentication, bundles, filters, some content pages, etc.
You have 2 option to fix it.
Install the latest 2013 version which have MVC5 support inbuilt (VS2013 missing MVC3 and ASPX file template (ASPX templates will be added in future))
stay with 2012 update 4. Read my old article to get it fixed http://geekswithblogs.net/anirugu/archive/2013/11/28/mvc-5-in-visual-studio-2012-update-4.-how-to.aspx
I'm not using 2012 now, but rather 2013, and I see the mvc 5 template there. However, I did this a while back before I had any mvc 5 templates showing up by installing mvc 5 first. You should be able to download from http://www.asp.net/mvc/mvc5.
Also make sure your target framework is set correctly.
Then I updated the web config like this:
You have to have the correct versions of the assemblies. I googled a lot to get this info, so it should still be available if you do some research.

Where to put custom view templates in MVC 3?

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.

Resources