C# 如何调试mvc脚手架文本模板文件?

C# 如何调试mvc脚手架文本模板文件?,c#,asp.net-mvc,t4,C#,Asp.net Mvc,T4,我将自定义脚手架文件添加为CustomCreate.tt。在mvc“添加视图”对话框中,当我在对话框中选择自定义模板时,出现错误。如何调试文本模板文件?如果使用Visual Studio=VS 2012的版本 调试T4模板的更好方法是添加断点,与普通的CSharp和VB代码一样。要在T4模板上运行调试器,只需在解决方案资源管理器中右键单击它,然后从上下文菜单中选择调试T4模板 以下文章还将帮助您解决常见的T4问题: 请注意,这仅适用于VS2012及更高版本。因此,我提到的第一种方法适用于使用旧

我将自定义脚手架文件添加为CustomCreate.tt。在mvc“添加视图”对话框中,当我在对话框中选择自定义模板时,出现错误。如何调试文本模板文件?

如果使用Visual Studio
  • 在.tt文件中包括System.Diagnostics命名空间(
  • 在您试图生成的代码周围放置一个try catch,并将eny异常输出到
    Trace.TraceError
  • 如果运行模板时发生错误,则错误详细信息应显示在Visual Studio控制台窗口中
  • 您可以以以下内容为例:

    <#@ template language="C#" HostSpecific="True" #>
    <#@ output extension="cs" #>
    <#@ import namespace="System" #>
    <#@ parameter type="System.String" name="ControllerName" #>
    <#@ parameter type="System.String" name="ControllerRootName" #>
    <#@ parameter type="System.String" name="Namespace" #>
    <#@ parameter type="System.String" name="AreaName" #>
    <#@ import namespace="System.Diagnostics" #>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    <#        try{  #>
    namespace <#=            Namespace #>
    {
        public class <#=            ControllerName #> : Controller
        {
            //
            // GET: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/
            public ActionResult Index()
            {
                return View();
            }
    
            //
            // GET: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Details/5
            public ActionResult Details(int id)
            {
                return View();
            }
    
            //
            // GET: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Create
            public ActionResult Create()
            {
                return View();
            }
    
            //
            // POST: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Create
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            //
            // GET: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Edit/5
            public ActionResult Edit(int id)
            {
                return View();
            }
    
            //
            // POST: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Edit/5
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            //
            // GET: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Delete/5
            public ActionResult Delete(int id)
            {
                return View();
            }
    
            //
            // POST: <#=            (!String.IsNullOrEmpty(AreaName)) ? ("/" + AreaName) : String.Empty #>/<#=            ControllerRootName #>/Delete/5
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
    
    <# }
        catch(Exception e)
        {
            Trace.TraceError(e.ToString());
            throw;
        }
    #>
    
    
    使用制度;
    使用System.Collections.Generic;
    使用System.Linq;
    使用System.Web;
    使用System.Web.Mvc;
    名称空间
    {
    公共类:控制器
    {
    //
    //获取://
    公共行动结果索引()
    {
    返回视图();
    }
    //
    //获取http://Details/5
    公共行动结果详细信息(int id)
    {
    返回视图();
    }
    //
    //获取://Create
    公共操作结果创建()
    {
    返回视图();
    }
    //
    //POST://Create
    [HttpPost]
    公共操作结果创建(FormCollection集合)
    {
    尝试
    {
    //TODO:在此处添加插入逻辑
    返回操作(“索引”);
    }
    抓住
    {
    返回视图();
    }
    }
    //
    //获取http://Edit/5
    公共操作结果编辑(int id)
    {
    返回视图();
    }
    //
    //POST://Edit/5
    [HttpPost]
    公共操作结果编辑(int id,FormCollection集合)
    {
    尝试
    {
    //TODO:在此处添加更新逻辑
    返回操作(“索引”);
    }
    抓住
    {
    返回视图();
    }
    }
    //
    //GET://Delete/5
    公共操作结果删除(int id)
    {
    返回视图();
    }
    //
    //POST://Delete/5
    [HttpPost]
    公共操作结果删除(int-id,FormCollection集合)
    {
    尝试
    {
    //TODO:在此处添加删除逻辑
    返回操作(“索引”);
    }
    抓住
    {
    返回视图();
    }
    }
    }
    }
    
    请注意,try-catch块包围了类的所有模板代码

    如果使用Visual Studio>=VS 2012的版本 调试T4模板的更好方法是添加断点,与普通的CSharp和VB代码一样。要在T4模板上运行调试器,只需在解决方案资源管理器中右键单击它,然后从上下文菜单中选择调试T4模板

    以下文章还将帮助您解决常见的T4问题:


    请注意,这仅适用于VS2012及更高版本。因此,我提到的第一种方法适用于使用旧版本Visual Studio的开发人员。

    当我在自定义视图创建中出错时。错误页面显示异常…我希望在CustomCreate.tt中设置breakepoint并检查我的codes@M.Azad请参阅更新的答案。您想要做的事情只有在Visual Studio 2012或更高版本中才能实现。如果您使用的是旧版本,我建议您使用Trace.TraceInformation在代码中的某些点显示变量的值。@M.Azad您能够调试T4模板吗?