简体   繁体   中英

How can I debug mvc scaffolding Text Template file?

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

If you use a version of Visual Studio < VS 2012

You can try the following:

  1. Include the System.Diagnostics namespace in your .tt file ( <#@ import namespace="System.Diagnostics" #> )
  2. Put a try-catch around the code that you are attempting to generate, and output eny exceptions to Trace.TraceError
  3. If you run the template and an error occurs, the error details should appear in your Visual Studio Console Window.

You can look at the following as an example:

<#@ 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;
    }
#>

Notice that the try-catch block surrounds all of the templating code for the class.

If you use a version of Visual Studio >= VS 2012

A better way to debug your T4 template is to add breakpoints, as with normal CSharp and VB code. To run the debugger on your T4 template simply right-click it in the solution explorer and select Debug T4 Template from the context menu.

The following article will also help you to solve common T4 issues: Debugging a T4 Text Template

Notice that this is only available for VS2012 and up. As such, the first method I mentioned will be suitable for developers using older versions of Visual Studio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM