How to install T4MVC in the ASP.NET MVC application

1) Create NOT empty ASP.NET MVC application


ASP.NET MVC application

2) Right click on the project -> select “Manage NuGet Packages…”

Manage NuGet Packages

3) Find T4MVC and click “Install” button

Install T4MVC

Now you have installed T4MVC feature!


So, if we click “Run Custom Tool”

Run Custom Tool

T4MVC will generate C# entities for our controllers, views, images and other content which you added in the

i.e. now you should not use simple hard coded strings like this:

Action from AccountController:

         public ActionResult LogOff()



            return RedirectToAction(“Index”, “Home”);


or _LoginPartial.cshtml view


        <li>@Html.ActionLink(“Register”, “Register”, “Account”, routeValues: null, htmlAttributes: new { id = “registerLink” })</li>

        <li>@Html.ActionLink(“Log in”, “Login”, “Account”, routeValues: null, htmlAttributes: new { id = “loginLink” })</li>



now you must use T4MVC generated entities to avoid hard coding. In the next example for LogOff method we do not hard code Index action for Home controller.

        public virtual ActionResult LogOff()

            return RedirectToAction(MVC.Home.Index());

and on the view we do not hardcore Register and Account actions


        <li>@Html.ActionLink("Register", MVC.Account.Register(), htmlAttributes: new { id = "registerLink" }) </li>

        <li>@Html.ActionLink("Log in", MVC.Account.Login(), htmlAttributes: new { id = "loginLink" })</li>

so, we’ll get exception now, if build solution with no Register action. It’s really great option do not hardcore names of views or actions. We may also use picture, css, or attach javascript files on the views or behind code through “Links”.


Example: _LoginPartial.cshtml view

<img src="@Links.Content.Site_css" />

Also you can add your custom folder and all content in this one might be generated into classes, but after you have added new folder do not forget to add this one to the to the <StaticFilesFolders> node to the <FileFolder>


you can also set to false SplitIntoMultipleFiles option, to avoid generation of many classes, as it displayed on the figure



T4MVC classes