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 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
ASP.NET MVC and T4 and NerdDinner
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With