I have put my t4 template in a CodeTemplate\MyTemplateFolder folder but can't get it to work from there.
The problem is that I need to access a folder, called models, that is in the root of the project.
Ie:
Project Root Folder\CodeTemplates\MyTemplateFolder\MyTemplate.tt
and I need to access a file in:
Project Root Folder\Models
However, I can't figure out how to use this.Host.ResolvePath to get at the Models folder from my template.
Any suggestions?
Use of:
var relativePathModelsFolder = @"..\..\Models";
var path = this.Host.ResolvePath(relativePathModelsFolder);
gives me the absolute path to the root folder with ....\Models stuck on the end.
I don't seem to be able to traverse back up to the root, and then into my Models folder.
This seems a simpler way to me:
({
<#
var projectPath = Host.ResolveAssemblyReference("$(ProjectDir)");
var viewPath = projectPath + "Views";
#>
view:"<#=viewPath #>",
}
)
I have solve the issue by getting the project then getting the path then adding the folder:
Getting the project:
Project GetLibraryProject(string tier)
{
return VisualStudioHelper.GetAllProjects().Where(x => x.Name == tier).First();
}
Getting the path:
string GetProjectPath(Project project)
{
var pathProjectFile = project.FullName;
return pathProjectFile.Replace(project.Name + ".csproj", "");
}
Putting it all together:
var project = GetLibraryProject("MyMvcApp.MyClassLibrary");
var path = GetProjectPath(project);
var myUsefulPath = path + @"MyFolderParent\MyFolder";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With