利用VS2010,给自己的ASP.NET MVC添加自己的控制器模板

利用VS2010,给自己的ASP.NET MVC添加自己的控制器模板,也就是新建一个Controller的时候,默认的代码。

步骤如下:

1. 新建一个MVC项目,在根目录下依次新建CodeTemplates>AddController文件夹。

2. 在AddController文件夹下新建Controller.tt文件,截图如下:

2011040816510518.jpg

3. 编写你的模板,我编写的如下:

<#@ template language="C#" HostSpecific="True" #>
<#@ import namespace="" #>
<#

var mvcHost = (MvcTextTemplateHost)(Host);

// You can use Reflector to see the available MvcTextTemplateHost properties, it's in this assembly:
// "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Microsoft.VisualStudio.Web.Mvc.3.0.dll"

#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace <#= mvcHost.Namespace #>
{
    public partial class <#= mvcHost.ControllerRootName #>Controller : Controller
    {

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
		[ValidateAntiForgeryToken]
        public ActionResult Index(IndexModel model)
        {
            if (!ModelState.IsValid)
			{
                return View();
			}
            return null;
        }
    }
	
	public partial class <#= mvcHost.ControllerRootName #>Controller
    {
        public class IndexModel
        {
            // public string Property1 { get; set; }
        }
    }
}

4. 右键Controller.tt属性,清空“自定义工具”对应的值,刚开始我没有清空,编译时就报错,后来参考http://stackoverflow.com/questions/1045066/mvctexttemplatehost-not-found 后才知道要清空。截图如下:

2011040816521481.jpg

5. 编译生成,OK!

2011040816523098.jpg

以后就可以添加自己的Controller模板了!

谢谢浏览!

转载于:https://www.cnblogs.com/Music/archive/2011/04/08/add-my-controller-template-in-asp-net-mvc.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值