I've been using T4MVC for some time now and love the "explicit HtmlHelpers for rendering partials" feature, which by default is switched off. I am using T4MVC version 2.6.40.
I recently upgraded to MVC3 and noticed that no explicit HtmlHelpers are generated for Razor partials, so I looked at the source code of the T4MVC text template and found a method named "GetPartials" which has a line of code as folows:
var parts = GetControllers()
.Select(m => m.ViewsFolder)
.SelectMany(m => m.Views)
.Where(m => m.Value.EndsWith(".ascx"));
So it is clear that Razor views are not supported.
I'd also like to mention that when running the T4 template (right-click > run custom tool) I get a compiler warning stating: "The C# 2.0 and C# 3.5 compilers are no longer supported. Templates will always be compiled with the version 4 compiler instead of 'v3.5' as specified."
This relates to line 18 where the template language attribute has a value of "C#v3.5". Why does it have to have an explicit version dependency? Can it not just be "C#"?
Apologies for asking two seperate questions in one post.
I just released T4MVC 2.6.42 to address this. You can get it from Codeplex or from NuGet.
Note that in order to have a razor file be detected as a partial by T4MVC, its name needs to start with an underscore (e.g. _foo.cshtml). Without this restriction, we would end up creating helper methods for all views, which would pollute things and not add value. Note that prefixing partial Razor views with _ is generally recommended by the MVC team.
As for the warning, it's unrelated and is benign. To get rid of it, just change language="C#v3.5" to language="C#". I can't make that change in the official version as that would make it break when running on 3.5 (and I don't want to maintain two separate versions just for that).
The compiler warning you receive is just that, a warning. It is not preventing T4MVC from working.
As for supporting Razor, you've found the appropriate code in the template - simply modify it.
var parts = GetControllers()
.Select(m => m.ViewsFolder)
.SelectMany(m => m.Views)
.Where(m => m.Value.EndsWith(".ascx") || m.Value.EndsWith(".cshtml") || m.Value.EndsWith(".vbhtml"));
I'd give that a try.
It's just a T4 Template, not magic. All it contains is simple c# code that gets project info from the Visual Studio environment and generates some fairly simple c# code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With