One of the things people like the most about my ASP.NE MVC 2 Ninja Black Belt Tips video, besides the Bill Cosby sweater, is the tip where I show how to modify the default CodeTemplates that are used in Code Generation in ASP.NET MVC (either version).
人们最喜欢我的ASP.NE MVC 2 Ninja Black Belt Tips视频的事情之一,是Bill Bill Cosby毛衣,它是向我展示如何修改ASP.NET MVC中代码生成中使用的默认CodeTemplates的技巧(任一版本)。
Eilon mentioned it on an internal mailing list this week so I thought it'd be worth surfacing again in case you haven't heard of this, Dear Reader.
Eilon本周在内部邮件列表中提到了它,因此,亲爱的读者,如果您还没有听说过,我认为值得再次提出。
Soon, we'll see even more powerful, flexible, and fun ways to customize your own Scaffolding in ASP.NET MVC 3, so keep an eye out, 'cause it's coming.
很快,我们将看到更强大,灵活且有趣的方法来在ASP.NET MVC 3中自定义自己的脚手架,因此请注意,因为它来了。
将CodeTemplates带到您的项目本地 (Bring the CodeTemplates local to your project)
When you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?
当您使用Visual Studio“工具”(即对话框和内容)来添加视图或添加控制器时,实际上是在执行T4模板并生成一点代码。 它从哪里开始,如何更改?
Try this. Create a new ASP.NET MVC project.
尝试这个。 创建一个新的ASP.NET MVC项目。
Now, go to:
现在,转到:
C:\Program Files (or x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp (or Visual Basic)\Web\MVC (or 2) 3\CodeTemplates
C:\ Program Files(或x86 )\ Microsoft Visual Studio 10.0 \ Common7 \ IDE \ ItemTemplates \ CSharp(或Visual Basic )\ Web \ MVC(或2 )3 \ CodeTemplates
See all those folders? Turns out everything under CodeTemplates are T4 templates that can be either modified in place or even better, can be brought local to your project. Local CodeTemplates will override the global ones.
查看所有这些文件夹? 下CodeTemplates原来一切都是T4模板,可以就地或者修改或者甚至更好,可以把您的本地项目。 本地代码模板将覆盖全局代码模板。
Copy that folder (or just subfolders like AddController, or AddView) to your ASP.NET project. I do this by dragging from Explorer directly into the Visual Studio Solution Explorer as seen here:
将该文件夹(或只是子文件夹,如AddController或AddView)复制到您的ASP.NET项目中。 我是通过直接从资源管理器拖动到Visual Studio解决方案资源管理器中来实现的,如下所示:
You might get some errors when you drop the folder. Ignore them and instead, select the Templates using Ctrl-Click, then right click and select Properties. (You can delete any templates you won't be using.)
删除文件夹时,您可能会遇到一些错误。 忽略它们,而是使用Ctrl-Click选择模板,然后右键单击并选择“属性”。 (您可以删除不需要的任何模板。)
Now, see where it says Custom Tool? Clear that string out completely. You're tell Visual Studio that you don't want these T4 Templates to be run during a build. They will only be called manually by the Tooling dialogs like Add View and Add Controller.
现在,在哪里显示“自定义工具”? 彻底清除该字符串。 您告诉Visual Studio,您不希望在构建过程中运行这些T4模板。 它们只能通过“工具”对话框(如“添加视图”和“添加控制器”)手动调用。
You change these templates, or in the case of Add View you can make your own. For example here I've made a new "My Awesome List.tt" as a copy of List.tt and it shows up in the Add View dialog.
您可以更改这些模板,或者在添加视图的情况下,可以自己制作。 例如,在这里,我创建了一个新的“ My Awesome List.tt”作为List.tt的副本,它显示在“添加视图”对话框中。
Now I can not only totally customize what gets generated from the Add Controller and Add View dialogs, I can add custom templates to the drop down. Most importantly, these changes live in the CodeTemplates folder of my project and are carried along with my project in Source Control so others on my team or company can use them as well.
现在,我不仅可以完全自定义从“添加控制器”和“添加视图”对话框中生成的内容,还可以向下拉列表中添加自定义模板。 最重要的是,这些更改位于我项目的CodeTemplates文件夹中,并且随我的项目一起在Source Control中进行,因此我的团队或公司中的其他人也可以使用它们。
Have you customized yours? Enjoy.
你有定制你的吗? 请享用。
相关链接 (Related Links)
T4 (Text Template Transformation Toolkit) Code Generation - Best Kept Visual Studio Secret
The Weekly Source Code 43 - ASP.NET MVC and T4 and NerdDinner