【发布时间】:2010-01-12 08:33:01
【问题描述】:
我正在尝试使用 T4 模板获取 Views 文件夹中的文件夹名称,但它不断给我以下错误:
错误 3 编译转换:名称“服务器”在当前上下文中不存在 c:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.tt 20 47
错误 4 命名空间不直接包含字段或方法等成员 C:\Projects\LearningASPMVC\LearningASPMVCSolution\LearningMVC\StronglyTypedViews.cs 1 1 LearningMVC
这是 T4 模板:
<#@ 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 #> { }
<# } #>
}
更新:使用物理路径让它工作:
<#@ 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() #>";
<# } #>
<# } #>
}
}
【问题讨论】:
-
问题是HttpContext.Current为null!
标签: t4