T4MVC 何时将支持具有显式 HtmlHelpers 的 Razor 来渲染部分?
我已经使用 T4MVC 一段时间了,并且喜欢“用于渲染部分的显式 HtmlHelpers”功能,该功能默认情况下处于关闭状态。我使用的是 T4MVC 版本 2.6.40。
我最近升级到MVC3,注意到没有为Razor部分生成显式的HtmlHelpers,所以我查看了T4MVC文本模板的源代码,发现一个名为“GetPartials”的方法,其中有一行代码如下:
var parts = GetControllers()
.Select(m => m.ViewsFolder)
.SelectMany(m => m.Views)
.Where(m => m.Value.EndsWith(".ascx"));
所以很清楚不支持 Razor 视图。
我还想提一下,当运行 T4 模板(右键单击 > 运行自定义工具)时,我收到编译器警告,指出:“不再支持 C# 2.0 和 C# 3.5 编译器。模板将始终使用版本 4 编译器而不是指定的“v3.5”。”
这与第 18 行相关,其中模板语言属性的值为“C#v3.5”。为什么它必须有明确的版本依赖性?难道不只是“C#”吗?
很抱歉在一篇文章中提出两个单独的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚发布了 T4MVC 2.6.42 来解决这个问题。您可以从 Codeplex 或 NuGet 获取它。
请注意,为了让 T4MVC 将 razor 文件检测为部分文件,其名称需要以下划线开头(例如 _foo.cshtml)。如果没有这个限制,我们最终将为所有视图创建辅助方法,这会污染事物并且不会增加价值。请注意,MVC 团队通常建议在部分 Razor 视图前添加 _ 前缀。
至于警告,它是无关的并且是良性的。要摆脱它,只需将 language="C#v3.5" 更改为 language="C#" 即可。我无法在官方版本中进行此更改,因为这会使其在 3.5 上运行时崩溃(并且我不想为此维护两个单独的版本)。
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).
您收到的编译器警告只是一个警告。它不会阻止 T4MVC 工作。
至于支持 Razor,您已经在模板中找到了相应的代码 - 只需修改它即可。
我会尝试一下。
这只是一个 T4 模板,而不是魔法。它包含的只是简单的 C# 代码,用于从 Visual Studio 环境获取项目信息并生成一些相当简单的 C# 代码。
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.
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.