T4 code generation 2 in MVC development ---- vs template extension, t4 ----

Source: Internet
Author: User

T4 code generation 2 in MVC development ---- vs template extension, t4 ----

In the previous article, I introduced the basic knowledge related to the T4 template in the Text Template-one of the four versions of code generated by MVC, and introduced how to use the T4 template to add a view in MVC. After knowing how to use the T4 template, I naturally thought about how to scale the T4 template that comes with vs and add some custom T4 templates. There are two ways to expand the T4 template of:

A. directly in the vs template directory (C: \ Program Files (x86) \ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ Extensions \ Microsoft \ Web \ Mvc \ Scaffolding \ Templates \ MvcView) add. The template added in this method belongs to the IDE level and can be applied to all projects. B. create the "CodeTemplates \ MvcView" directory in the project and add a template to it. The template added by this method is only applicable to the project. The two methods are described below.

Add a T4 template to the vs template directory.

This method is simple. You only need to add the T4 template file and name it according to the file naming rules. cs. t4 name. For vb, use. vb. t4 naming. After the addition is complete, we add more new ones in the vs template. For example, we add a new Test in the directory. cs. you can add a t4 template without restarting vs, and the added template can be used in all projects.

Before and after adding vs template changes:

  

  

Create a "CodeTemplates \ MvcView" directory in the project to add a template

Sometimes we want to create a T4 template that is only used in the project. For example, we think that the List template provided by vs is not suitable for the project, so we have customized a new List template, the new List template is only used for the current project. If you add the required information to the vs directory, you must add the necessary information on the name of the new List template file to distinguish it from the original List template, after adding a new view in vs after a long time, a bunch of List Templates will appear, and each time you add a view, you must differentiate them. Obviously this is unwise, we cannot replace or delete a List template as it may be used by other projects.

Creating a T4 template for use only in the project is simple. You only need to add the "CodeTemplates \ MvcView" directory structure in the project, and then add the T4 template to it, even if the newly added template and the template in the vs directory are used with the same name, the template is generated using the template in the project. This does not affect the template in the vs directory. Note that the directory structure must be consistent with "CodeTemplates \ MvcView". If you want to create a T4 template using the Imports and ModelMetadataFunctions template files that come with, to create an include command for the T4 template, use the absolute directory or copy the Imports and ModelMetadataFunctions template files in the vs directory to the CodeTemplates \ MvcView directory of the project.

For example, the following TestViewModel is used to generate the distribution view. The content of TestViewModel is as follows:

public class TestViewModel    {        public string Name { get; set; }        public string Age { get; set; }    }

First, use the Create template that comes with vs to generate the view. The dialog box for generating the view is configured as follows:

The generated view is as follows:

Then, create the "CodeTemplates \ MvcView" directory structure in the project, and import the Imports in the vs directory. include. t4 and ModelMetadataFunctions. cs. include. copy the t4 template file to the CodeTemplates \ MvcView directory of the project, Create a text file under "CodeTemplates \ MvcView" in the project, and change it to Create. cs. t4. For the template content, we directly copy the Create. cs. in the t4 content, "

In this case, you can see that there is only one Create template in the template option, and the view is generated without changing other parameters as follows:

  

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.