我试图动态添加控制器,但问题是他们的动作也是动态的,所以我不能简单地使用通用控制器。
我通过msdn挖掘,发现覆盖方法Apply@IApplicationModelConvention
似乎是我最好的选择。但是,路由系统从未找到我添加的控制器。
这就是我的尝试:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(opts =>
{
opts.Conventions.Add(new ApplicationDescription("mdesc"));
opts.Conventions.Add(new ControllerDescription("mctrl"));
})
...
}
public class ApplicationDescription : IApplicationModelConvention
{
private readonly string _description;
public ApplicationDescription(string description)
{
_description = description;
}
public void Apply(ApplicationModel application)
{
var basec = application.Controllers[0];
var cm = new ControllerModel(typeof(MyController).GetTypeInfo(), basec.Attributes);
application.Controllers.Add(cm);
application.Properties["description"] = _description;
}
}
来源:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-2.0