MVC中t4模板在视图中的运用,创建自定义视图的t4模板

本文介绍如何在MVC项目中创建并使用自定义的T4模板来生成视图,根据不同版本的MVC选择安装相应的模板包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MVC中t4模板在视图中的运用,创建自定义视图的t4模板


.NET中mvc中创建视图时可以使用模板

这样:


查阅了很多资料 发现都不能把自定义的模板在创建的模板中显示出来。多番研究终于发现和MVC的版本有很大关系。


具体做法:

  根据你MVC的版本 在“工具>>NuGet包管理器>>程序包管理控制台”中输入:

  MVC3:install-package mvc3codetemplatescsharp
  MVC4:install-package mvc4codetemplatescsharp
  MVC5:install-package mvc5codetemplatescsharp






MVC5为例:

执行后就可导入模板包到当前项目中。





项目中生成这样的文件则表示成功,在MvcView文件中加入自定义模板



 在创建时根据你自定义的模板创建



这样就算引入了模板,至于模板里的内容。。 根据自个需求写吧 ( ・ˍ・) 


asp.net mvc 自定义视图引擎 public ThemeableRazorViewEngine() { AreaViewLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", }; AreaMasterLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", }; AreaPartialViewLocationFormats = new[] { //themes "~/Areas/{2}/Themes/{3}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Themes/{3}/Views/Shared/{0}.cshtml", //default "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" }; ViewLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", //Admin //"~/Administration/Views/{1}/{0}.cshtml", //"~/Administration/Views/Shared/{0}.cshtml", }; MasterLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; PartialViewLocationFormats = new[] { //themes "~/Themes/{2}/Views/{1}/{0}.cshtml", "~/Themes/{2}/Views/Shared/{0}.cshtml", //default "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml", //Admin //"~/Administration/Views/{1}/{0}.cshtml", //"~/Administration/Views/Shared/{0}.cshtml", }; FileExtensions = new[] { "cshtml" }; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值