About T4 templates and asp.net MVC

Source: Internet
Author: User
Tags visual studio visual studio 2010

Microsoft Visual Studio contains a code generation engine called the T4 (abbreviation for the text Template Conversion Toolkit). You may have used the T4 template in Visual Studio, and you don't even know that the templates are already working in the background. In this article, I'll briefly introduce the T4 template and explain how ASP.net MVC uses this technology. I'll also explain how to customize the T4 template to use the MVC framework to improve your day-to-day productivity.

The basic idea behind the template toolkit is to parse the input file and convert it to an output file. The input file is a template (a text file with a. tt file name extension). The output file also contains text, which can be C # code, Visual Basic code, Web form code, markup, or any other content you need to generate.

The easiest way to watch the T4 demo is to create a new project in Visual Studio. I will generate C # code in this article, so you can use any project type that compiles C # code. After you open the project, right-click the item, and then select Add | New Project ". Select text file from the Add New Item dialog box (Visual Studio 2008 does not have a project template dedicated to T4, but is available in Visual Studio 2010), and then name the file Simple.tt (make sure to use the. tt extension). After you load the file into your project, you will immediately see the Simple.cs file displayed in the Solution Explorer window after simple.tt. (see Figure 1).

Figure 1 The C # file following the T4 template

Simple.tt and Simple.cs files are initially empty. If you right-click the Simple.tt file, and then select Properties, you will see the custom tool that Visual Studio specifies texttemplatingfilegenerator as a file (see Figure 2). This builder is the T4 engine that converts the template file into a file that consists entirely of C # code.

Figure 2 The properties of the T4 template

To enable the template to perform the required action, add the following code:

<#@ template language= "c#v3.5" #> <#@ Assembly name=
"System.Web.Mvc.DLL" #> <#@
Import Namespace= "SYSTEM.WEB.MVC" #> public
    
class Test
{
<# for (int i = 0; i < 5; i++) {#> 
  public in T prop<#= i #> {get; set;}
<#} #>
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.