问题描述
我使用的是 ASP.NET MVC5 和 VS2013
I'm using ASP.NET MVC5 and VS2013
我试图从
C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDEVWDExpressItemTemplatesCSharpWebMVC 4CodeTemplates
在其中包含子文件夹AddController"、AddView"和 T4 模板,然后我修改了一些模板,但是当我尝试添加 View 和 Controller 时没有任何反应..
with subfolders "AddController", "AddView" and T4 templates inside them, then I modified some templates, but nothing happened when I tried to add View nor Controller..
推荐答案
首先,您的计算机上似乎安装了 Visual Studio 2013 和 2012.我尝试查找您提供的路径,但找不到.在您的道路上,您似乎正在尝试使用 MVC4 模板.这是我的路径:
First, it looks like you have Visual Studio 2013 and 2012 both installed on your computer. I tried looking up the path you provided, I couldn't find it. On your path it looks like you're trying to use MVC4 templates. Here is my path:
C:Program Files (x86)Microsoft Visual Studio 12.0
Common7IDEExtensionsMicrosoftWebMvcScaffoldingTemplates
以下是我为 BaseController 自定义脚手架模板的方法:
Below is how I customized my scaffold template for the BaseController:
直接在项目文件夹中创建名为CodeTemplates"的文件夹.
Create folder called "CodeTemplates" directly in the project folder.
转到下面的路径,找到 MvcControllerWithActions 文件夹,将所有内容复制到文件夹CodeTemplates"
Go to below path, find MvcControllerWithActions folder copy all to folder "CodeTemplates"
C:Program Files (x86)Microsoft Visual Studio 12.0
Common7IDEExtensionsMicrosoftWebMvcScaffoldingTemplates
转到项目中的 CodeTemplates -> MvcControllerWithActions.
Go to CodeTemplates -> MvcControllerWithActions in your project.
MvcControllerWithActions中有两个文件,Controller.cs.t4
和Controller.vb.t4
,如果你用过C#,可以删除Controller.vb.t4
.
There are two files, Controller.cs.t4
and Controller.vb.t4
in MvcControllerWithActions, if you used C#, you can delete Controller.vb.t4
.
打开Controller.cs.t4
文件,修改Controller名称为BaseController,如下图:
Open the Controller.cs.t4
file, modify the Controller name to BaseController, like below:
public class <#= ControllerName #> : BaseController
{
}
现在,当您尝试使用添加新脚手架项"创建 MVC5 控制器时,它将使用您自定义的模板.
Now, when you try to create MVC5 Controller using "add new scaffold item", it'll use the template you customized.
希望有帮助.
这篇关于如何在 ASP.NET MVC5 中创建自定义脚手架模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!