Modifying T4 code templates for ASP.NET MVC to get rid of full namespace reference

1k views Asked by At

We're using (there is need to tell where the files are, thanks) custom T4 code templates on creating a view or controller. Default implementation makes this kind first row.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ProjectNameHere.ViewModels.ViewModelClass>" %> 

We have already ViewModel and MVC namespace defined in the Web.config, so I would like code template to generate this.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ViewModelClass>" %>

Any suggestions how modify the default templates to get that kind of results? Which of the template lines actually generate these?

More information

I know where the files are and modifications has been made. Problem is that in the template they're using this

string mvcViewDataTypeGenericString = (!String.IsNullOrEmpty(mvcHost.ViewDataTypeName)) ? "<" + mvcHost.ViewDataTypeName + ">" : String.Empty;

It seems that ViewDataTypeName contains full namespace reference. I would like get just name of the class (in this case ViewModel class name)

1

There are 1 answers

1
stoic On BEST ANSWER

You can find the templates here:

*\Microsoft Visual Studio \Common7\IDE\ItemTemplates\CSharp\Web\MVC 2\CodeTemplates*

There you can edit the generated code for each template, the template lines vary for each template so i cannot tell you, but you will spot them immidiately.

EDIT

The source for Web.Extensions is not available, but you can make use of relector and reflect on \Microsoft Visual Studio \Common7\IDE\Microsoft.VisualStudio.Web.Extensions.dll assembly to see what "mvcHost" gives you :)

You will then notice a ViewDataType property which is a "Type", and you should then be able to create some logic around it to get the class name.