I am trying to get the folder names inside the Views folder using the T4 template and it continues to give me the following errors:
Error 3 Compilation of conversion: the name "Server" does not exist in the current context c: \ Projects \ LearningASPMVC \ LearningASPMVCSolution \ LearningMVC \ StronglyTypedViews.tt 20 47
Error 4 There are no immediate elements in the namespace, 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: it works 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() #>";
<# } #>
<# } #>
}
}
source
share