修改 ASP.NET MVC 的 T4 代码模板以摆脱完整的命名空间引用
我们正在使用(需要告诉文件在哪里,谢谢)自定义 T4 代码模板来创建视图或控制器。默认实现使这种类型成为第一行。
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectNameHere.ViewModels.ViewModelClass>" %>
我们已经在 Web.config 中定义了 ViewModel 和 MVC 命名空间,所以我希望代码模板来生成它。
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ViewModelClass>" %>
有什么建议如何修改默认模板以获得这种结果吗?哪一个模板行实际上生成了这些?
更多信息
我知道文件所在位置以及已进行的修改。问题是在模板中他们使用这个
string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty;
ViewDataTypeName 似乎包含完整的命名空间引用。我只想获取类的名称(在本例中为 ViewModel 类名)
We're using (there is need to tell where the files are, thanks) custom T4 code templates on creating a view or controller. Default implementation makes this kind first row.
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectNameHere.ViewModels.ViewModelClass>" %>
We have already ViewModel and MVC namespace defined in the Web.config, so I would like code template to generate this.
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ViewModelClass>" %>
Any suggestions how modify the default templates to get that kind of results? Which of the template lines actually generate these?
More information
I know where the files are and modifications has been made. Problem is that in the template they're using this
string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty;
It seems that ViewDataTypeName contains full namespace reference. I would like get just name of the class (in this case ViewModel class name)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在此处找到模板:
*\Microsoft Visual Studio \Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates*
您可以在其中编辑每个模板生成的代码,每个模板的模板行有所不同,所以我无法告诉你,但你会立即发现它们。
编辑
Web.Extensions 的源不可用,但您可以使用 relector 并反思
\Microsoft Visual Studio \Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll 程序集来查看“mvcHost”为您提供了什么:)
然后您会注意到 ViewDataType 属性是“类型”,然后您应该能够围绕它创建一些逻辑来获取类名。
You can find the templates here:
*\Microsoft Visual Studio \Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates*
There you can edit the generated code for each template, the template lines vary for each template so i cannot tell you, but you will spot them immidiately.
EDIT
The source for Web.Extensions is not available, but you can make use of relector and reflect on
\Microsoft Visual Studio \Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll assembly to see what "mvcHost" gives you :)
You will then notice a ViewDataType property which is a "Type", and you should then be able to create some logic around it to get the class name.