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!

T4MVC

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 T4MVC.tt.settings.xml

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

Action from AccountController:

         public ActionResult LogOff()

        {

            WebSecurity.Logout();

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

        }

or _LoginPartial.cshtml view

    <ul>

        <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>

    </ul>

 

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()
        {
            WebSecurity.Logout();

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

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

   

    <ul>
        <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>
    </ul>

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 T4MVC.tt.settings.xml to the <StaticFilesFolders> node to the <FileFolder>

  <StaticFilesFolders>
    <FileFolder>Scripts</FileFolder>
    <FileFolder>Content</FileFolder>
    <FileFolder>MyFolder</FileFolder>
  </StaticFilesFolders>

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

</MvcSettings> 
 <SplitIntoMultipleFiles>False</SplitIntoMultipleFiles>
</MvcSettings>

 

T4MVC classes