【问题标题】:Files generated by T4 templates are not included in projectT4 模板生成的文件不包含在项目中
【发布时间】:2017-12-31 00:35:48
【问题描述】:

我正在使用 T4 模板(运行时生成)为我的项目(控制器、实体和视图)生成几个 .cs 文件,这些文件需要由我的控制器的操作动态创建,它将特定数据传递给这些模板。

这是我在操作中生成它们的方式:

 CustomEntity entity = new CustomEntity(entityName, propertyMap);
 String entityContent = entity.TransformText();
 System.IO.File.WriteAllText(Server.MapPath("~") + "\\Models\\" + entityName + ".cs", entityContent);

 CustomEntityController controller = new CustomEntityController(entityName);
 String controllerContent = controller.TransformText();
 System.IO.File.WriteAllText(Server.MapPath("~") + "\\Controllers\\" + entityName + "Controller.cs", controllerContent);

它们是正确生成的,但是它们没有包含在我的项目中,所以无论如何我都不能在我的项目中使用它们。只需右键单击文件并“包含在项目中”即可解决此问题,但我需要在我的项目中自动访问它们。

我该如何解决这个问题?

【问题讨论】:

  • 您需要自己将文件添加到项目中(例如使用EnvDTE)。

标签: c# asp.net-mvc t4


【解决方案1】:

this answer 中所述,您可以将动态生成的 .cs 文件包含到具有递归通配符包含的 Visual Studio 项目中。这可以通过手动编辑 csproj 文件来完成。

语法如下(根据您的情况检查正确的路径):

<ItemGroup>
  <Compile Include="Models\**\*.cs" />
  <Compile Include="Controllers\**\*.cs" />
</ItemGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多