I'm trying to add the T4MVC templates to my project, but I'm experiencing some problems. I went to Codeplex and downloaded the latest version of T4MVC, and according to the instructions I just copied the two files T4MVC.tt
and T4MVC.Settings.t4
into the root of my web application.
我正在尝试将T4MVC模板添加到我的项目中,但我遇到了一些问题。我去了Codeplex并下载了最新版本的T4MVC,根据说明我刚将两个文件T4MVC.tt和T4MVC.Settings.t4复制到我的Web应用程序的根目录中。
Immediately, I got the following errors:
我立刻得到了以下错误:
From T4MVC.cs
(generated file):
从T4MVC.cs(生成的文件):
A namespace cannot directly contain members such as fields or methods
From T4MVC.tt
(the code generating template):
从T4MVC.tt(代码生成模板):
Compiling transformation: The type or namespace name 'ITextTemplatingEngineHost' could not be found (are you missing a using directive or an assembly reference?)
When I open T4MVC.cs
, it only contains one line:
当我打开T4MVC.cs时,它只包含一行:
ErrorGeneratingCode
I found this post that suggests just building again, but that solution does not solve my problem - in fact, it doesn't change a thing. What should I do?
我发现这篇文章建议再次构建,但该解决方案并不能解决我的问题 - 事实上,它并没有改变一件事。我该怎么办?
3 个解决方案
#1
17
OK, I figured it out. The problem was that apparently since the last release of the T4MVC package, Microsoft changed the location of the ITextTemplateHost
interface, so I needed to import another namespace. Also, the .dll
files with these namespaces aren't imported in the default ASP.NET MVC template project. This is what I did to make it work:
好的,我明白了。问题是,自上次发布T4MVC软件包以来,Microsoft改变了ITextTemplateHost接口的位置,因此我需要导入另一个名称空间。此外,具有这些命名空间的.dll文件不会导入默认的ASP.NET MVC模板项目中。这就是我做的工作:
-
Add references to the following .dll files (search paths on my machine in brackets):
添加对以下.dll文件的引用(括号中我的计算机上的搜索路径):
-
Microsoft.VisualStudio.TextTemplating.10.0.dll (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.10.0.dll)
Microsoft.VisualStudio.TextTemplating.10.0.dll(C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.TextTemplating.10.0 \ v4.0_10.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.10.0.dll)
-
Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll)
Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll(C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.TextTemplating.Interfaces.10.0 \ v4.0_10.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.TextTemplating.Interfaces .10.0.dll)
-
-
Make sure the following namespaces are both imported in
T4MVC.tt
(just follow the syntax that's already in the file).确保以下命名空间都在T4MVC.tt中导入(只需按照文件中已有的语法)。
-
Microsoft.VisualStudio.TextTemplating
Microsoft.VisualStudio.TextTemplating
-
Microsoft.VisualStudio.TextTemplating.Interfaces
Microsoft.VisualStudio.TextTemplating.Interfaces
-
-
Delete all generated code files (they'll appear if you expand the
T4MVC.tt
in Solution Explorer).删除所有生成的代码文件(如果在解决方案资源管理器中展开T4MVC.tt,它们将会出现)。
-
Build project. If no files are generated, open
T4MVC.tt
, edit something, don't save and build. That should do it!建立项目。如果没有生成文件,请打开T4MVC.tt,编辑内容,不要保存和构建。应该这样做!
#2
3
ADDENDUM - ASP.NET MVC 3 RTM
As an addendum to this issue, if you have just installed ASP.NET MVC 3 RTM, you may hit similar problems in projects that were created using one of the MVC 3 Release Candidates.
作为附录这个问题,如果你刚刚安装ASP.NET MVC 3 RTM,你可能会碰到在被使用MVC 3发布候选者之一创建的项目类似的问题。
To solve:
Make sure that you have T4MVC.2.6.40 or later installed using NuGet.
确保使用NuGet安装了T4MVC.2.6.40或更高版本。
Ie, reinstall T4MVC using Tools >> Library Package Manage. In the Console, type:
即,使用工具>>库包管理重新安装T4MVC。在控制台中,键入:
uninstall-package t4mvc
and then:
接着:
install-package t4mvc
to get the latest version.
获得最新版本。
You will now probably get the following error in the T4MVC.tt file:
您现在可能会在T4MVC.tt文件中收到以下错误:
A namespace cannot directly contain members such as fields or methods
Open the file, make it dirty (eg, by deleting the comment with the squiggly line) then close WITHOUT SAVING!!! See David Ebbo's answer, t4mvc.tt NEEDS to be dirty!
打开文件,使其变脏(例如,通过删除带有波浪线的注释)然后关闭而不保存!看到David Ebbo的回答,t4mvc.tt需要变脏!
Recompile and go get a life.
重新编译并获得生命。
#3
2
Seems you got it working, but let me add a few points here.
看起来你已经开始工作,但是我在这里补充几点。
About the need to import Microsoft.VisualStudio.TextTemplating.Interfaces, I added a comment about that at the top of T4MVC.tt. But yeah, it's easy to miss.
关于导入Microsoft.VisualStudio.TextTemplating.Interfaces的需要,我在T4MVC.tt的顶部添加了关于它的评论。但是,很容易错过。
Now for the confusing part: while those interfaces indeed moved to that different namespace in Beta2, the T4 team had a change of heart and decided to move them back again to the original namespace to avoid confusion. So when the next RC build comes out, this namespace will be gone.
现在对于令人困惑的部分:虽然这些接口确实移动到Beta2中的那个不同的命名空间,但是T4团队改变了主意并决定将它们再次移回原始命名空间以避免混淆。因此,当下一个RC构建出来时,这个命名空间将会消失。
About the need to import DLLs, I'm surprised that you need this, as it works for me without it.
关于导入DLL的需要,我很惊讶你需要这个,因为它没有它对我有用。
Final note: in step #4, it should be harmless if you save here. What happens is that T4MVC has logic that keeps itself dirty, so even if you save, you'll see it marked as unsaved (this can be turned off in the settings file).
最后注意事项:在步骤#4中,如果你保存在这里,它应该是无害的。会发生什么事情是T4MVC具有使自己变脏的逻辑,所以即使你保存,你也会看到它被标记为未保存(这可以在设置文件中关闭)。
Hope this helps!
希望这可以帮助!
#1
17
OK, I figured it out. The problem was that apparently since the last release of the T4MVC package, Microsoft changed the location of the ITextTemplateHost
interface, so I needed to import another namespace. Also, the .dll
files with these namespaces aren't imported in the default ASP.NET MVC template project. This is what I did to make it work:
好的,我明白了。问题是,自上次发布T4MVC软件包以来,Microsoft改变了ITextTemplateHost接口的位置,因此我需要导入另一个名称空间。此外,具有这些命名空间的.dll文件不会导入默认的ASP.NET MVC模板项目中。这就是我做的工作:
-
Add references to the following .dll files (search paths on my machine in brackets):
添加对以下.dll文件的引用(括号中我的计算机上的搜索路径):
-
Microsoft.VisualStudio.TextTemplating.10.0.dll (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.10.0.dll)
Microsoft.VisualStudio.TextTemplating.10.0.dll(C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.TextTemplating.10.0 \ v4.0_10.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.10.0.dll)
-
Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll)
Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll(C:\ Windows \ Microsoft.NET \ assembly \ GAC_MSIL \ Microsoft.VisualStudio.TextTemplating.Interfaces.10.0 \ v4.0_10.0.0.0__b03f5f7f11d50a3a \ Microsoft.VisualStudio.TextTemplating.Interfaces .10.0.dll)
-
-
Make sure the following namespaces are both imported in
T4MVC.tt
(just follow the syntax that's already in the file).确保以下命名空间都在T4MVC.tt中导入(只需按照文件中已有的语法)。
-
Microsoft.VisualStudio.TextTemplating
Microsoft.VisualStudio.TextTemplating
-
Microsoft.VisualStudio.TextTemplating.Interfaces
Microsoft.VisualStudio.TextTemplating.Interfaces
-
-
Delete all generated code files (they'll appear if you expand the
T4MVC.tt
in Solution Explorer).删除所有生成的代码文件(如果在解决方案资源管理器中展开T4MVC.tt,它们将会出现)。
-
Build project. If no files are generated, open
T4MVC.tt
, edit something, don't save and build. That should do it!建立项目。如果没有生成文件,请打开T4MVC.tt,编辑内容,不要保存和构建。应该这样做!
#2
3
ADDENDUM - ASP.NET MVC 3 RTM
As an addendum to this issue, if you have just installed ASP.NET MVC 3 RTM, you may hit similar problems in projects that were created using one of the MVC 3 Release Candidates.
作为附录这个问题,如果你刚刚安装ASP.NET MVC 3 RTM,你可能会碰到在被使用MVC 3发布候选者之一创建的项目类似的问题。
To solve:
Make sure that you have T4MVC.2.6.40 or later installed using NuGet.
确保使用NuGet安装了T4MVC.2.6.40或更高版本。
Ie, reinstall T4MVC using Tools >> Library Package Manage. In the Console, type:
即,使用工具>>库包管理重新安装T4MVC。在控制台中,键入:
uninstall-package t4mvc
and then:
接着:
install-package t4mvc
to get the latest version.
获得最新版本。
You will now probably get the following error in the T4MVC.tt file:
您现在可能会在T4MVC.tt文件中收到以下错误:
A namespace cannot directly contain members such as fields or methods
Open the file, make it dirty (eg, by deleting the comment with the squiggly line) then close WITHOUT SAVING!!! See David Ebbo's answer, t4mvc.tt NEEDS to be dirty!
打开文件,使其变脏(例如,通过删除带有波浪线的注释)然后关闭而不保存!看到David Ebbo的回答,t4mvc.tt需要变脏!
Recompile and go get a life.
重新编译并获得生命。
#3
2
Seems you got it working, but let me add a few points here.
看起来你已经开始工作,但是我在这里补充几点。
About the need to import Microsoft.VisualStudio.TextTemplating.Interfaces, I added a comment about that at the top of T4MVC.tt. But yeah, it's easy to miss.
关于导入Microsoft.VisualStudio.TextTemplating.Interfaces的需要,我在T4MVC.tt的顶部添加了关于它的评论。但是,很容易错过。
Now for the confusing part: while those interfaces indeed moved to that different namespace in Beta2, the T4 team had a change of heart and decided to move them back again to the original namespace to avoid confusion. So when the next RC build comes out, this namespace will be gone.
现在对于令人困惑的部分:虽然这些接口确实移动到Beta2中的那个不同的命名空间,但是T4团队改变了主意并决定将它们再次移回原始命名空间以避免混淆。因此,当下一个RC构建出来时,这个命名空间将会消失。
About the need to import DLLs, I'm surprised that you need this, as it works for me without it.
关于导入DLL的需要,我很惊讶你需要这个,因为它没有它对我有用。
Final note: in step #4, it should be harmless if you save here. What happens is that T4MVC has logic that keeps itself dirty, so even if you save, you'll see it marked as unsaved (this can be turned off in the settings file).
最后注意事项:在步骤#4中,如果你保存在这里,它应该是无害的。会发生什么事情是T4MVC具有使自己变脏的逻辑,所以即使你保存,你也会看到它被标记为未保存(这可以在设置文件中关闭)。
Hope this helps!
希望这可以帮助!