我将Custom脚手架文件添加为CustomCreate.tt。在mvc添加视图对话框中,当我在对话框中选择我的自定义模板时出现错误。如何调试我的文本模板文件?
You can try the following:
<#@ import namespace="System.Diagnostics" #>
) Trace.TraceError
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.
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.