Recent Posts - Subscribe Here

T4 Transformations for ASP.NET paths

Published - Wednesday, February 16, 2011

T4 is an engine built into Visual Studio for code generation. It has been around since VS 2005 and I have been playing with it for a while. In the past I used it to generate unit tests just after compile time that used the templates and reflection to automatically test a particularly nasty DB persistence layer for one of our company’s products. It ended up uncovering about 4000 inconsistencies and potential bugs.

More recently I have created a template that automatically creates the paths for the pages in your project. I hate to hard code values, but sometimes in it seems you cannot get around hard coding references. This will eliminate that dependency.

It works by iterating over the file structure of your project and finds classes that inherit from Page. It then generates a partial class for each page that has a static string with the full path to that page. It also generates a static class for each type of image resource with a static string with its path as well. In this way you can reference all of the other pages in your project like this <%=MyPage.Path %> or your resources <%=Images.JPG.MyPicture%>. If you ever decide to move the pages or resources around, for whatever reason you can freely do so without worrying about breaking hard coded references.

Here is the link to the file that does it.

Labels:, C#, code-generation, T4

Reading lists

Here is some of what I have been reading lately

Software Books

Non Fiction


I am Kevin Kinnett, a software developer in Atlanta, GA.More about me.

profile for aceinthehole on Stack Exchange, a network of free, community-driven Q&A sites

Innovative Architects

I am a Senior Software Engineer/Consultant working for Innovative Architects.

Innovative Architects is a Gold Certified Microsoft Consulting company located in Duluth, GA, right outside of Atlanta.

I am mainly engaged with BizTalk development projects, although I also specialize solving business needs using ASP.NET, and Sharepoint among others.


I my previous position I was a Software Engineer working for ADAM Inc.

Adam specializes in software for employers, benefits brokers and healthcare organizations which provide customizable health solutions to help hospitals, managed care organizations, and consumer web sites become an integral part of the online consumer healthcare experience.

I was responsible for several mission critical applications, including being on the 2 person team that was charged with the final stages of the rewrite of the company’s flagship product


In a previous position I was a Software Engineer working for Nexidia

Nexidia has applied years of research and development to deliver a comprehensive range of solutions for audio and video search. Nexidia works with some of the world’s largest contact centers, rich media companies, government agencies, and legal firms to help them realize the amazing possibilities now discoverable in audio and video content.

I was part of serveral .NET application development projects including standalone tools, applications, and frameworks used to perform system testing, automated functional testing, load testing, and automated UI testing. Also applications for continous integration and automated deployment of configurable complex data sets into installed products.


While attending Kennsaw State University I majored in Computer Science

While in the STARS program, I developed a software application to keep track of the audio/visual equipment the department loans out, which is currently in use and written in Java Swing and uses a MySQL database for persistence

In the morning