5

My controller is inherited from anothe controller which doesn't have default constructor. T4MVC generates the following constructor which assumes base controller has default constructor:

protected MyControllerController(Dummy d) { }

How can I resolve this problem? Interesting enough, according to this page, version 2.4.00 "fixed issue when a base controller doesn't have a default ctor". I also found this SO question, but my base controller is not generic.

Community
  • 1
  • 1
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • I was able to repro your issue with version 2.10.0... I think you should post the issue on the T4MVC issue tracker, and until it fixed use the workaround from the linked SO question. Namely add a default constructor to your base class. – nemesv Jul 24 '12 at 07:09
  • 1
    I'm out, but I'll try to investigate this toward the end of the week. – David Ebbo Jul 24 '12 at 10:14

1 Answers1

6

This should work fine if you make your base controller abstract. I assume that it never needs to be used directly as a controller? If it does, then you can always create another non-abstract derived class to handle that.

David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Yeah, there is a couple ways to workaround it but I am working on the scenario (I have described it here: http://stackoverflow.com/questions/11576606/asp-net-mvc-reuse-of-controllermodelviewroutes) which looks pretty reasonable. Wouldn't it be nice if T4MVC supported that without hacks? – SiberianGuy Jul 29 '12 at 04:02
  • Not following you here. Are you using the base type directly or not? If not, then marking it as abstract is not a hack, it's the right thing to do. – David Ebbo Jul 29 '12 at 12:15
  • I am using it directly. I just pass some specific dependencies from derived controller to it. – SiberianGuy Jul 29 '12 at 16:32
  • 1
    I'm having the same issue, but marking it as abstract does *not* solve the problem. Adding an empty constructor will break dependency injection, so that is not an option either. – Nilzor Mar 04 '14 at 09:11
  • Are you saying that making it abstract has no effect on the generated code? I would expect that to cause a default ctor to be generated in the abstract base. Yet you are describing adding a default ctor as an *alternate* thing you tried, so something here doesn't add up. – David Ebbo Mar 04 '14 at 17:36
  • -1 as it does not work if you make base constructor abstract: public abstract class BaseController : Controller { protected BaseController(ILogger logger); } public partial class HomeController : BaseController { public HomeController(ILogger logger) : base(logger) { } public virtual ActionResult Index() { return View(); } } – Skorunka František May 19 '14 at 17:17
  • Marking my base controller as `abstract` is also not working for me. – DLeh Sep 29 '14 at 12:31