Put All methods in one .tt file and use it in another t4 files in CodeTemplates of MVC

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

I use ASP.NET MVC 3 Custom CodeTemplates I need to put some methods in one t4 file and then use it in all my t4 templates.

So this is my general.tt file:

<#@ template language="C#" #> <#@ assembly name="System" #> <#@ assembly name="System.Core" #> <#@ assembly name="System.ComponentModel.DataAnnotations" #> <#@ assembly name="System.Data" #> <#@ assembly name="System.Data.Entity" #> <#@ assembly name="System.Data.Linq" #> <#@ assembly name="System.Xml.Linq" #> <#@ import namespace="System" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="System.ComponentModel.DataAnnotations" #> <#@ import namespace="System.ComponentModel" #> <#@ import namespace="System.Data.Linq.Mapping" #> <#@ import namespace="System.Data.Objects.DataClasses" #> <#@ import namespace="System.Reflection" #> <#@ import namespace="Microsoft.VisualStudio.Web.Mvc.Scaffolding.BuiltIn" #>  <#+  public class XXD {     public string getitNow(){         return "Yup thats it!";     } }  #>

Then I use it in create.tt file like this:

<#@ include file="general.tt" #>  ...  <#+  private string GetitNow(){    XXD xx = new XXD();    return xx.getitNow(); }

but there is an error when I want to Add View Use create template:

error: Loading the included file 'general.tt' returned a null or empty string. The transformation will not be run.

So where is the problem? what is your suggestion to do this?

回答1:

Last time I checked, the MVC scaffolding still used a custom implementation of the ITextTemplatingEngineHost, which implements the include directive differently than the ITextTemplating service in Visual Studio. I suggest for you to submit a Connect bug, describe the problem you are experiencing and ask to change the T4 implementation in MVC to the built-in ITextTemplating service, which does not have this and other limitations.



回答2:

That normally means that the file isn't found. Is general.tt in a different path to create.tt?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!