T4MVC

Home / T4MVC

I’ve used T4MVC within my MVC projects for a long, long time now. It’s one of those extremely useful utilities that probably gets forgotten.

At any rate, I’m not sure if I ever mentioned why I like T4MVC, but here are a few reasons.


1. Strongly typed references to all assets – images, css files, etc etc

I only thought about this when looking at a typical reference to an image within a view earlier:

<img src="../content/images/logo_white.png" alt="Some Title" />

That’s OK, but if the image is moved, or if I want to use the image in different areas, it can fail. T4MVC eliminates this issue by giving you a about global “Link” object to which all asset paths (strings) are attached. So, the above becomes this:

<img src="@Links.Content.images.logo_white_png" alt="Some Title" />

If the image is removed, and the T4MVC templates are rebuilt, then the next time that view is rendered, we’ll get a compiler/Razor error.

2. Strongly typed references to all MVC views, actions, controllers, and areas

This is one of the best features of T4MVC. The “MVC” object that is generated by T4MVC contains links to all View names, Action Names, Controllers/Controller names and even Controller actions. So, instead of using string literals with Url/Html helpers, ActionLinks, etc, we can reference by strong types.

As an example, this:

@Html.ActionLink("GoToIndex", "Index")

Becomes this:

@Html.ActionLink("GoToIndex", MVC.SomeArea.SomeController.ActionNames.Index)

Again, the next time the T4MVC templates are updated, if the reference action name / controller is moved are renamed, the Razor view rendering would fail.

3. Another side-effect of having all of your view names strongly typed, you can do some tricks with Reflection to retrieve a list of all views. Those views, in term, could be served up, in a strongly-typed manner, on a request for, let’s say a template. I use this method to create a single controller in all of my SPA/JavaScript applications to be able to request views by name via a single controller action. The controller action looks something like this:

[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
public virtual ActionResult Index(string name)
{
    List<FieldInfo> instanceProperties = MVC.Template.Views.ViewNames.GetType().GetFields().ToList();
    var resourceProp = instanceProperties.FirstOrDefault(x => x.Name.ToLowerInvariant() == name.ToLowerInvariant());
    return PartialView(resourceProp.GetValue(MVC.Template.Views.ViewNames) as string);
}

To utilize it, for example, in an Angular app with, I define the view path url simply as ‘Template/{name}.’ This method makes it very handy since after I create a view and put it into my Templates view folder, I run T4MVC’s generation tool and then the templates are available from the controller action to the client app. Pretty neat..

I highly recommend using T4MVC in your MVC projects: T4MVC on Nuget

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.