Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 211
Tomas Aschan Avatar asked Dec 15 '09 09:12

Tomas Aschan


3 Answers

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:\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)

  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!

like image 139
Tomas Aschan Avatar answered Nov 15 '22 15:11

Tomas Aschan


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.

To solve:

Make sure that you have T4MVC.2.6.40 or later installed using NuGet.

Ie, reinstall T4MVC using Tools >> Library Package Manage. In the Console, type:

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:

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!

Recompile and go get a life.

like image 24
awrigley Avatar answered Nov 15 '22 15:11

awrigley


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.

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.

About the need to import DLLs, I'm surprised that you need this, as it works for me without it.

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).

Hope this helps!

like image 2
David Ebbo Avatar answered Nov 15 '22 14:11

David Ebbo