Reputation: 18465
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,
Upvotes: 6
Views: 3058
Reputation: 5843
You need to override the T4 template and may also use scaffolding for productivity. Here us more info : MvcScaffolding: Overriding the T4 Templates
Upvotes: 1
Reputation: 2782
You should be able to modify the controller T4 templates located in a folder like this:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 3\CodeTemplates\AddController
Simply change the line
public class <#= mvcHost.ControllerName #> : Controller
to
public class <#= mvcHost.ControllerName #> : BaseController
Also, the links provided by Asif are useful.
Upvotes: 3
Reputation: 11409
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 #> : Controller
should become public class <#= mvcHost.ControllerName #> : BaseController
More details can be found on Scott Hanselman's blog
Upvotes: 4
Reputation: 13150
You need custom code generation
in MVC
.
The following link might be helpful.
Modifying the default code generation/scaffolding templates in ASP.NET MVC
and also
Upvotes: 5