Thursday, 13 October 2011
ASP NET MVC 3 T4 Template Properties
Here are the properties that ASP.NET MVC 3.0 provides to T4 templates when you use the Add View and Add Controller dialogs inside Visual Studio 2010.
Add Controller
| Property Name | Type | Description |
|---|---|---|
| AddActionMethods | Boolean | Adds action methods to the generated controller class. |
| AreaName | System.String | The name of the Area that the controller is created for. |
| ContextType | System.Type | The type of the data context. |
| ControllerName | String | The name of the controller class that will be generated. |
| ControllerRootName | String | The name of the controller class excluding the Controller part at the end of the name. |
| EntitySetName | String | Name of the property on the data context class containing the set of entities. |
| ModelType | System.Type | The type of the model class specified in the Add Controller dialog. |
| Namespace | String | Namespace that will be used for the generated controller class. |
| PrimaryKeys |
PrimaryKey[]
(Microsoft. VisualStudio. Web.Mvc. Scaffolding. BuiltIn) |
Primary keys for the model. See table at end for PrimaryKey properties. |
| RelatedProperties |
Dictionary<String,
RelatedModel (Microsoft. VisualStudio. Web. Mvc. Scaffolding. BuiltIn)> |
Related properties on the model. See table at end for RelatedModel properties. |
Add View
| Property Name | Type | Description |
|---|---|---|
| AreaName | System.String | The name of the Area that the view is being created for. |
| ContentPlaceHolderIDs | List<string> | List of content place holder IDs in the master page. |
| IsContentPage | Boolean | True if the generated view will be created with a master page or a Razor layout page. |
| IsPartialView | Boolean | True if the generated view is a partial view (e.g. an ASP.NET user control). |
| MasterPageFile | String | Master page file or Razor layout to be used with view (e.g. ~/Views/Shared/Site.Master). |
| Namespace | String | Namespace that will be used for the generated view. |
| PrimaryContentPlaceHolderID | String | Primary content place holder ID to be used when creating a view using a master page. |
| ReferenceScriptLibraries | Boolean | True if checked in the Add View dialog. |
| ViewDataType | System.Type | The view model's type. |
| ViewDataTypeName | String | Fully qualified name for the view model's type. |
| ViewName | String | Name of the view. |
Common Properties
| Property Name | Type | Description |
|---|---|---|
| AssemblyPath | List<string> | List of assemblies referenced by the project and the project's output assembly. Internal use. |
| Errors |
CompilerErrorCollection
(System.CodeDom.Compiler) |
Used to stored errors that occur whilst processing the T4 template. Internal use. |
| FileEncoding | System.Text.Encoding | The encoding of the file that will be generated. |
| FileExtension | String | Not set. |
| FrameworkVersion | System.Version | The .NET framework version. |
| OutputFileExtension | System.String | The extension of the file that will be generated. |
| TemplateFile | System.String | The full path to the T4 template file being used. |
Primary Key Properties
| Property Name | Type | Description |
|---|---|---|
| Name | String | |
| ShortTypeName | String | |
| Type | System.Type |
Related Model Properties
| Property Name | Type | Description |
|---|---|---|
| DisplayPropertyName | String | |
| EntitySetName | String | |
| ForeignKeyPropertyName | String | |
| PrimaryKey | String | |
| PropertyName | String | |
| TypeName | String |
Tuesday, 4 October 2011
T4 Import Directive - Namespace Alias
How do you specify a namespace alias when using the Import directive in a T4 template?
The Import directive in a T4 template is used to bring a namespace into scope for any code that will be run when processing the T4 template. Here is an example template that imports the System.IO namespace without using a namespace alias.
<#@ template language="C#" #> <#@ output extension=".txt" #> <#@ import namespace="System.IO" #> <# string text = File.ReadAllText(@"d:\MyFile.txt"); #> File content: <#= text #>
The T4 template reads all the text in the d:\MyFile.txt which is then saved in the file generated by the template.
If we want to use a namespace alias we can specify it in the import directive in a similar way to how you can with C# or VB.NET. Here is the example template modified so that an alias of IO is used for the System.IO namespace.
<#@ template language="C#" #> <#@ output extension=".txt" #> <#@ import namespace="IO = System.IO" #> <# string text = IO.File.ReadAllText(@"d:\temp\test.xsd"); #> File content: <#= text #>
Why does this Work?
T4 templates use the CodeDom to generate C# or VB.NET code which is then executed as the template is processed. When the T4 templating engine processes the Import directive it creates a CodeNamespaceImport object and passes it the Namespace attribute's value. The CodeNamespaceImport object supports generating a namespace alias when you specify a string of the form "Alias = Namespace" and pass this to its constructor.