Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
953 views
in Technique[技术] by (71.8m points)

visual studio 2010 - Cannot get T4MVC to work with VS2010 and ASP.NET MVC 2

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.

Immediately, I got the following errors:

From T4MVC.cs (generated file):

A namespace cannot directly contain members such as fields or methods

From T4MVC.tt (the code generating template):

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:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

  1. Add references to the following .dll files (search paths on my machine in brackets):

    • Microsoft.VisualStudio.TextTemplating.10.0.dll (C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.10.0.dll)

    • Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll (C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.Interfaces.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.TextTemplating.Interfaces.10.0.dll)

  2. Make sure the following namespaces are both imported in T4MVC.tt (just follow the syntax that's already in the file).

    • Microsoft.VisualStudio.TextTemplating

    • Microsoft.VisualStudio.TextTemplating.Interfaces

  3. Delete all generated code files (they'll appear if you expand the T4MVC.tt in Solution Explorer).

  4. Build project. If no files are generated, open T4MVC.tt, edit something, don't save and build. That should do it!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...