When I create a new controller in Visual Studio with MVC It generate automatically the following code :
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}
My Default1Controller inherit from Controller but I work with a BaseController class and I always have to remember to change the inheritance. Is it possible to Is it possible to modify or create a new template to automatically generate a more specific code for my project?
public class Default1Controller : BaseController
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}
Thank you,
You have to modify the T4 template that’s at the basis 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 modify the Controller.tt
The line
public class <#= mvcHost.ControllerName #> : Controllershould becomepublic class <#= mvcHost.ControllerName #> : BaseControllerMore details can be found on Scott Hanselman’s blog