English 中文(简体)
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?

最佳回答

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!

问题回答

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.

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!





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签