T4MVC没有将参数传递给基本控制器,因此生成的代码不会构建

Dav*_*ean 5 .net c# asp.net-mvc t4mvc asp.net-mvc-4

问题:

我构建时遇到以下错误:

"'.Controllers.ControllerBase'不包含带0个参数的构造函数"

我的基本控制器看起来像这样:

public abstract class ControllerBase : Controller
{
    public CompanyChannel<IAuthorizationService> authorizationServiceClient;
         public ControllerBase(CompanyChannel<IAuthorizationService> authService)
    {
        this.authorizationServiceClient = authService;
    }
}
Run Code Online (Sandbox Code Playgroud)

一个使用Base的示例控制器..

public partial class SearchController : ControllerBase
{
    protected CompanyChannel<IComplaintTaskService> complaintTaskServiceChannel;
    protected IComplaintTaskService taskServiceClient;      

    protected ComplaintSearchViewModel searchViewModel;

    #region " Constructor "

    public SearchController(CompanyChannel<IComplaintTaskService> taskService, CompanyChannel<IAuthorizationService> authService, ComplaintSearchViewModel viewModel)
        : base(authService)
    {
        searchViewModel = viewModel;
        this.complaintTaskServiceChannel = taskService;
        this.taskServiceClient = complaintTaskServiceChannel.Channel;
    }

    #endregion

    public virtual ActionResult Index()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎是绊倒T4MVC.

我应该不将params传递给基础构造函数吗?

Nas*_*eer 3

您的抽象类必须有一个默认构造函数。当子类中有任何不调用基类ctor的构造函数时,编译器将自动调用基类的默认ctor,因此基类中必须有一个。

以下演示将有助于理解 C# 中的 ctor 链接

class Base
{
    public Base()
    {
        Console.WriteLine("Base() called");
    }

    public Base(int x)
    {
        Console.WriteLine("Base(int x) called");
    }
}

class Sub : Base
{
    public Sub()
    {
        Console.WriteLine("Sub() called");     
    }
}
Run Code Online (Sandbox Code Playgroud)

并从您的 Main() 中创建

new Sub();
Run Code Online (Sandbox Code Playgroud)

并观察控制台输出