为什么80%的码农都做不了架构师?>>>
Microsoft.AspNetCore.Mvc.ApplicationParts 此名称空间管理所有的特征,同时这些特征所属不同的类库当中
Manages the parts and features of an MVC application.
ApplicationPartManager
IApplicationFeatureProvider 所有的特征对象都继承此接口
ApplicationPart 此对象主要就是设置各个特征的类库基本对象
ControllerFeatureProvider 此对象就是设置控制的特征对象
通过下面的方式我们就可以加载分离我们控制器类库
首先我们需要吧我们的控制器类库添加到管理器中
其次就是通过管理器循环所有的类库查找控制器特征对象
同时注意类库一定要包含Controller 文件名称
public void ConfigureServices(IServiceCollection services){
services.AddMvc().ConfigureApplicationPartManager( m => {
var feature = new ControllerFeature();
m.ApplicationParts.Add(new AssemblyPart(controllerAssembly));
m.PopulateFeature(feature);
services.AddSingleton(feature.Controllers.Select(t => t.AsType()).ToArray());
});
}
Microsoft.AspNetCore.Mvc.Controllers
public class ControllerFeatureProvider : IApplicationFeatureProvider
{
private const string ControllerTypeNameSuffix = "Controller";
///
public void PopulateFeature(
IEnumerable
ControllerFeature feature)
{
foreach (var part in parts.OfType
{
foreach (var type in part.Types)
{
if (IsController(type) && !feature.Controllers.Contains(type))
{
feature.Controllers.Add(type);
}
}
}
}
private static void AddDefaultFrameworkParts(ApplicationPartManager partManager)
{
var mvcTagHelpersAssembly = typeof(InputTagHelper).GetTypeInfo().Assembly;
if (!partManager.ApplicationParts.OfType
{
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcTagHelpersAssembly));
}
var mvcRazorAssembly = typeof(UrlResolutionTagHelper).GetTypeInfo().Assembly;
if (!partManager.ApplicationParts.OfType
{
partManager.ApplicationParts.Add(new FrameworkAssemblyPart(mvcRazorAssembly));
}
}