Creating a new controller class in Visual Studio with MVC?

When I create a new controller in Visual Studio with MVC, it automatically generates the following code:

public class Default1Controller : Controller
{
    //
    // GET: /Default1/

    public ActionResult Index()
    {
        return View();
    }

}

My Default1Controller inherits from Controller, but I work with the BaseController class, and I always need to remember about changing inheritance. Is it possible to modify or create a new template to automatically generate more specific code for my project?

public class Default1Controller : BaseController
{
    //
    // GET: /Default1/

    public ActionResult Index()
    {
        return View();
    }

}

Thank,

+5
source share
4 answers

You need to modify the T4 template that is at the heart of the Add Controller command.

Go to \ Microsoft Visual Studio 11.0 \ Common7 \ IDE \ ItemTemplates \ CSharp \ Web \ MVC 3 \ CodeTemplates \ AddController \ (replace with your version of VS and MCV) and change Controller.tt

public class <#= mvcHost.ControllerName #> : Controller public class <#= mvcHost.ControllerName #> : BaseController

Scott Hanselman blog

+4

custom code generation MVC.

, .

/ ASP.NET MVC

ASP.NET MVC T4 NerdDinner

+5

T4, :

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddController

public class <#= mvcHost.ControllerName #> : Controller

public class <#= mvcHost.ControllerName #> : BaseController

, Asif.

+3
+1

All Articles