把所有的方法在一个.TT文件和MVC的codeTemplates用它在另一个文件T4 [英] Put All methods in one .tt file and use it in another t4 files in CodeTemplates of MVC

查看:914
本文介绍了把所有的方法在一个.TT文件和MVC的codeTemplates用它在另一个文件T4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ASP.NET MVC 3 <一个href=\"http://www.hanselman.com/blog/ModifyingTheDefault$c$cGenerationscaffoldingTemplatesInASPNETMVC.aspx\"相对=nofollow>自定义codeTemplates 的我需要把一些方法在一个T4文件,然后在我所有的T4模板使用。

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.

所以这是我general.tt文件:

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!";
    }
}
 #>

然后,我用它create.tt文件是这样的:

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:

错误:加载包含文件'general.tt'返回空或空字符串。转型将无法运行。

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?

推荐答案

我最后一次检查时,MVC脚手架仍沿用ITextTemplatingEngineHost的自定义实现,它实现了包括指令不同于在Visual Studio中ITextTemplating服务。我建议你​​提交连接错误,描述您所遇到的问题,并要求在MVC改变T4落实到内置-in ITextTemplating服务,其不具有此和其他限制。

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.

这篇关于把所有的方法在一个.TT文件和MVC的codeTemplates用它在另一个文件T4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆