ASP.NET MVC custom T4 templates for views - asp.net-mvc

When I'm adding a new strongly-typed view to an ASP.NET MVC project, I can select between different view content templates (Create Details, Edit, List). I have read these are templates based on the Visual Studio T4 code generation engine.
How can I author and configure my own templates and let them appear in the available templates when adding a new view?

ASP.Net MVC - T4 Fun
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers

Create following folder structure:
{YourMVCProject}\CodeTemplates\AddView
Put your custom T4 templates (*.tt) in this folder. You will see the magic!

You might want to take a look at David Hayden's site: http://www.pnpguidance.net/Tag/T4.aspx -- He offers several screencasts on T4 and MVC that got me started. Also take a look at the T4 toolbox: (www.codeplex.com/t4toolbox). This provides additonal helpers for T4 templates.

You can add a package to your project using NuGet that will add the existing code templates to your project. From that point you could edit them or just create new ones in the same location and you should see them in the tooling.
http://nuget.org/List/Packages/Mvc3CodeTemplatesCSharp

Related

MVC empty WebSite project based on aspx pages

In Visual Studio we have a two ways that create the web projects.
I create the "WebSite" project, empty-project (like: File -> New -> WebSite... and so on).
After that, when the WebSite created I want to make it to MVC WebSite with ASPX, and not Razor pages.
[I decide create the MVC WebSite not Project Site, with this way, because the Visual Studio doesn't provide us WebSite with MVC template based aspx pages].
After creating some pages I want to create and integrate any Razor page.
Describe for question:
IF I attempt use in the Razor view page - " #model MyWebSite " it does not discovering, and I can't use with the ViewBag property later
Question:
What Can I do ?
What NuGet packeg I need install or what dll recourse I need adding to Bin folder of project.
Yes, you can use Razor with an existing ASP.NET WebSite. Simply open your website using the WebMatrix tool and start adding CSHTML files. One caveat is that if your website is using WebForms controls the WebMatrix tool will not provide any help working with them in existing aspx pages. Additionally, Razor does not support WebForms so you will not be able to add something like to a CSHTML file.

ASP.NET MVC template with webgrid instead of table

Is there a Asp.net MVC View Template that uses WebGrid instead of a <table> ?
I should recommend you to set up a T4 template that generates the code that you want for your MVC View Template. Its pretty easy, have a look at this guide and you should get up and going... http://msdn.microsoft.com/en-us/magazine/ee291528.aspx
Microsoft Visual Studio includes a code generation engine known as T4
(which is short for Text Template Transformation Toolkit). You’ve
probably already used T4 templates in Visual Studio without even
knowing they were working behind the scenes.

Side-by-Side Asp.Net and MVC Razor

We have an existing ASP.Net Web Application. I would like to create an ASP.Net MVC Razor Application where the two applications will work together. A single Master Page would contain menu items that can call .aspx pages as well as Razor .cshtml pages.
I have seen an example using MvcContrib Portable areas utilizing Routing. This particular example has .aspx pages in both (the MVC was not Razor).
Is there an example out there that will show the two running side-by-side and the MVC is Razor? It would be best if I could download a visual Studio Solution so that I can run this.
I am not sure if the MvcContrib way is the latest and best way to achieve this.
I do not want to go Hybrid!
You don't need any other external librarry. You can always convert the existing ASP.NET web forms Project to be a Hybrid one which uses webforms and MVC. You need to add the required MVC assembly references and make some changes to the web.config and you are all set. Scott has a simple and awesome blog post about this where he explains how to do the conversion.
I scribbled a note about how to enable the MVC specific Context menu( Add Controller / Add View) in the hybrid project after conversion here

How to add a custom project template in new Asp.Net MVC 3 project dialog?

I'm developing a plugin based application using MEF and Asp.NET MVC 3 and I want to create a custom project template to give third party developers when creating new plugins for application.
I can easily export it as a project template but since the host is developed in ASP.NET MVC 3, It seems more logical to be inside new Asp.Net MVC 3 project dialog.
Any ideas?
After some digging and 'reflecting', I got a template to successfully show up in the dialog. Most of the credit goes to this post: Add custom viewengine to New project dialog?
The key to getting it working on my machine (64bit) was the registry path mentioned in the answer in that post. I also found via Reflector there is a SupportsHTML5 key too, if you need that.
Here is a screen shot of my template:
and here are the registry keys (there is also a title and description at the MyOwnTemplate node)
If you look up your template directory for Visual Studio, and check out the template:
MvcWebApplicationProjectTemplatev3.0.cs.zip
(VS Install dir)\Common7\IDE\ProjectTemplates\CSharp\Web\1033
The thing to look at here is this line:
<WizardExtension>
<Assembly>Microsoft.VisualStudio.Web.Mvc.3.0, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>Microsoft.VisualStudio.Web.Mvc.UserInterface.MvcTemplateWizard</FullClassName>
</WizardExtension>
So they have got a custom Wizard working here.
these questions look to be similar:
Blank Asp.net MVC template
How to write an MVC3 Project Template that will offer option Razor or .aspx views
Basically, they say the wizard isn't open source but you could potentially write your own custom wizard.
EDIT:
here are a couple of page that tell you how to create your own wizard:
http://msdn.microsoft.com/en-us/library/ms185301.aspx
http://www.codeproject.com/KB/system/create_VS_wizard.aspx
I just wrote a blog post that covers how to do this and provides a zip file and a batch file with an example.
http://haacked.com/archive/2011/06/06/creating-a-custom-asp-net-mvc-project-template.aspx

Scaffolding controllers in ASP.NET MVC

What options do we have for scaffolding controllers in ASP.NET MVC (v2 and v3 RC)? I know about the code templates folder and T4 but it only allows creating one .tt file and thus, replacing the default controller template with your own but what I would need is different types of templates for various types of controllers.
I've heard that Rails has huge support for this. I'm also aware of the MvcScaffold package in MVC 3 (via NuGet) as described and developed by Scott Hanselman which supports ControllerWithContext and ControllerWithRepository but I would like to take this further and since the source code hasn't been published (at least I'm not aware of it) there isn't much I can do to customize it.
What do you guys use or do?
There is the Generic Controller (Controller<T>) technique. Big fan.

Resources