开发者

T4 Template and Server.MapPath

开发者_高级运维

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()  #>";

     <# } #>



     <#  } #>

     }




}


T4 templates are executed in a temporary context that visual studio creates, and is well outside your web application. That temporary context is intended for generating the output text file. It is not a web application in any way, and is unrelated to the web application your are authoring. As a result, System.Web.HttpContext doesn't have any value assigned, and MapPath() cannot be invoked.

Environment.CurrentDirectory isn't much help either, as the template is executed in some temporary folder.

What can you do? If you can use absolute paths, go ahead and do that. Otherwise, adding the hostspecific attribute in the <#@ template#> directive will allow you to the Host variable, and its ResolvePath() method. ResolvePath lets you resolve paths relative to the TT file itself.

For example (example.tt):

<#@ template language="C#" hostspecific="True" #>
<#@ output extension=".cs" #>
// <#=Host.ResolvePath(".")#>

Output (example.cs):

// C:\Users\myusername\Documents\Visual Studio 2008\Projects\MvcApplication1\MvcApplication1\.

Oleg Sych's post about the template directive has a section about the hostspecific attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜