T4 namespace cannot contain membes directly, such as fields or methods

Hi, I am just starting with T4 templates and I need to generate a javascript file based on the actions in my controller.

I got a code that I understood everything, forgetting about the controllers and actions. My only problem is that I get this error in the T4 template file and I do not understand this:

Transformation compilation: a namespace cannot directly contain elements such as fields or methods

This is my code:

<#@ template debug="false" hostspecific="false" language="C#" #> <#@ assembly name="System.Core" #> <#@ assembly name="$(TargetPath)" #> <#@ import namespace="System.Reflection" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ import namespace="eConnect.WebApi.Helpers.T4.ControllerDetails" #> <#@ import namespace="System.Web.Http;"#> <#@ output extension=".js" #> define(['services/logger', 'services/jsonDataService', 'services/config', 'services/cachingService'], function (logger, jsonDataService, config, cache) { var dataService = { }; return dataService; }); <# var controllers = ControllersInfo.GetControllers(); foreach(var controller in controllers) { Dictionary<string, ParameterInfo[]> actions = ControllersInfo.GetAllCustomActionsInController(controller, new HttpGetAttribute()); } #> 

There is also an external class that gets controllers and actions, but I don't think this is necessary for the current problem.

What am I doing wrong?

+6
source share
1 answer

You probably figured it out, but:

 <#@ import namespace="System.Web.Http;"#> 

Pay attention to ';'

Instead, write:

 <#@ import namespace="System.Web.Http"#> 
+5
source

Source: https://habr.com/ru/post/946650/


All Articles