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

來源:互聯網
上載者:User

本文連結:http://blogs.msdn.com/webdevtools/archive/2009/01/29/t4-templates-a-quick-start-guide-for-asp-net-mvc-developers.aspx

 

簡單地說,這是一套業界比較認可的代碼產生機制。T4的意思是:Text Template Transformation Toolkit

MVC使用了T4作為代碼產生模板,它的目的是讓使用者可以定製Controller和View模板。

這些模板在下面的目錄

F:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates\

模板裡面的大致如下

那麼,在模板中,可以使用的一些特殊變數主要包含什麼呢?請看下面的介紹


Add Controller:

Property Name

Type

Description

ItemName

System.String

The name of the Controller class, including the ‘Controller’ suffix

NameSpace

System.String

The namespace into which the Controller is being generated

ExtraActionMethods

System.Boolean

Indicates whether or not the user checked the option in the Add Controller dialog to get extra action methods

ControllerRootName

System.String

The name of the controller class without the ‘Controller’ suffix

Add View:

Property Name

Type

Description

ItemName

System.String

The name of the view (without extension), as typed in the Add View dialog

NameSpace

System.String

The default namespace of the view’s parent folder

IsViewUserControl

System.Boolean

Evaluates to true if the user chose a partial view in the Add View dialog

IsViewContentPage

System.Boolean

Evaluates to true if the user is creating a view with a master page

IsViewPage

System.Boolean

Evaluates to true if the user is creating a regular view page

MasterPage

System.String

Path to the master page the user chose in the dialog (to be used only when IsViewContentPage is true)

ContentPlaceholder

System.String

Name of the primary content place holder into which the generated content will be placed. This is the content place holder id the user typed into the Add View dialog

ContentPlaceHolders

System.Collections.Generic.List<System.String>

A list of all content place holder ID’s in the master page, if a master page was chosen for this view

LanguageExtension

System.String

The output file’s extension (including the period)

ViewDataTypeGenericString

System.String

This is a string that is used to output the generics clause for the ‘Inherits’ attribute in the view's directive (for strongly-typed views).  Example: “<MyType>” or “(Of MyType)”

ViewDataType

System.Type

This is a Type object representing the type to which a strongly-typed view is bound. It can be used to get information on the properties in the type and the like


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.