I am trying to get the folder names inside the Views folder using T4 Template and it keeps giving me the following errors:
Error 3 Compiling transformation: The name 'Server' does not exist in the current context c:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.tt 20 47
Error 4 A namespace does not directly contain members such as fields or methods C:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.cs 1 1 LearningMVC
Here is the T4 Template:
<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Web" #>
using System;
namespace StronglyTypedViews
{
<#
string[] folders = Directory.GetDirectories(Server.MapPath("Views"));
foreach(string folderName in folders)
{
#>
public static class <#= folderName #> { }
<# } #>
}
UPDATE: Got it working using the physical path:
<#@ template language="C#" debug="True" hostspecific="True" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Web" #>
<#@ assembly name="System.Web.Mvc" #>
<#@ import namespace="System.Web.Mvc" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Web" #>
using System;
namespace StronglyTypedViews
{
<#
string viewsFolderPath = @"C:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\";
string[] folders = Directory.GetDirectories(viewsFolderPath + "Views");
foreach(string folderName in folders)
{
#>
public static class <#= System.IO.Path.GetFileName(folderName) #> {
<#
foreach(string file in Directory.GetFiles(folderName)) {
#>
public const string <#= System.IO.Path.GetFileNameWithoutExtension(file) #> = "<#= System.IO.Path.GetFileNameWithoutExtension(file).ToString() #>";
<# } #>
<# } #>
}
}