12

I want to create T4 text templates to achieve code generation. All the tutorials I found on msdn suggest following to add a new text template: Add > New Item > Text Template, (eg https://msdn.microsoft.com/en-us/library/dd820620.aspx) but I don't see that option(Text Template) there. I am using ASP.NET core 1.0.

Is this issue related to VS2015 or ASP.NET core? If T4 templating is not supported in any of them, what's the best solution/alternative to achieve this?

(I want to generate typescript code from C# code), similar to this tutorial http://dotnetspeak.com/2015/02/typescript-models-creation-via-t4-templates

software_writer
  • 3,941
  • 9
  • 38
  • 64
  • I don't think this is fully supported yet. You can add .tt files to .net core class library projects and right click _Run Custom Tool_ on it. It will then be re-generated every time you change the .tt. See this [github issue](https://github.com/aspnet/Home/issues/272) or [this question](http://stackoverflow.com/questions/31563575/design-time-t4-templates-in-asp-net-5-vs-2015) – Daniel J.G. May 13 '16 at 10:29
  • Thank you for help. I am new to .net development so excuse me if this question sounds silly. Where/what is .net core class library project? – software_writer May 13 '16 at 19:06
  • When you add a new project to your solution, you can find the .net core class library under Visual C# -> Web -> Class Library (Package). This creates a library that you can use in other .net core projects (which can be libraries or other types of projects like a web site or a console application) – Daniel J.G. May 16 '16 at 10:41

2 Answers2

12

It was a bug in the visual studio in the early stages.

Now, with Visual Studio 2015 Update 3 (and I think with Update 2 too) I can easily run T4 templates, even in .NET Core / ASP.NET Core projects.

If there isn't the Add > New Item > Text Template option, you can add *.tt files as simple text files.

Give it a try. :)

Tóth Tibor
  • 1,526
  • 12
  • 21
  • Thanks for the update, appreciate it. There isn't `Add > New Item > Text Template`, but will try adding `*.tt` files. Hopefully that will work. – software_writer Aug 04 '16 at 21:37
  • Let me know about your success :) – Tóth Tibor Aug 05 '16 at 07:37
  • Hey @TóthTibor and MatthiasBurger sorry for late reply, my initial attempt did not work, and due to some other reasons I haven't been able to work on this recently. I will update asap if I make progress. – software_writer Nov 24 '16 at 18:31
  • 1
    @TóthTibor and MatthiasBurger, I am able to generate text template now. Though VS 2015 doesn't give me an option to add "Text Template" file, I simply created a new file, and saved it as `.tt`, then it's functioning as a text template. Thanks for your help. :) – software_writer Dec 17 '16 at 01:13
  • 3
    November 2019, VS 2019, ASP.Net Core 3.0. It still **IS** a bug there. – Wolfrevok Cats Nov 19 '19 at 20:07
  • September 2020 VS community edition. Still couldn't add TT file to Web project. Just adding Manually. – Praveen Sep 13 '20 at 15:45
0

My issue was that the "Run Custom Tool" was not appearing when I manually added my TT file to the project. I need to set "Custom Tool" to "TextTemplatingFileGenerator" on the Properties tab for the TT file.

Right Click on the TT file and select Properties enter image description here

Garry English
  • 5,070
  • 1
  • 36
  • 23