问题
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?
回答1:
ASP.Net MVC - T4 Fun
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
回答2:
Create following folder structure:
{YourMVCProject}\CodeTemplates\AddView
Put your custom T4 templates (*.tt) in this folder. You will see the magic!
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/1009067/asp-net-mvc-custom-t4-templates-for-views