是否有与旧 WebApi IHttpControllerTypeResolver 等效的 AspNetCore?

2024-02-10

在WebApi中,您可以替换内置的IHttpControllerTypeResolver,您可以按照您喜欢的方式找到您想要的 Api 控制器。

在使用 MVC 的 AspNetCore 中,PartsManager 和 FeatureManager 存在令人困惑的混乱,其中某些地方与控制器有关。我能找到的所有文档和讨论似乎都假设您是一名致力于 MVC 本身的开发人员,并且您已经了解 ApplicationPartManager 和 ControllerFeatureProvider 之间的区别,而无需解释任何内容。

在最简单的示例中,我特别想做的是启动 AspNetCore 2.0 Kestrel 服务器的实例,并让它仅解析预配置的硬编码单个控制器。我明确地不希望它做正常的发现之类的事情。

在 WebApi 中,您只需执行以下操作:

public class SingleControllerTypeResolver : IHttpControllerTypeResolver
{
    readonly Type m_type;

    public SingleControllerTypeResolver(Type type) => m_type = type;

    public ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver) => new[] { m_type };
}

// ...
// in the configuration:
config.Services.Replace(typeof(IHttpControllerTypeResolver), new SingleControllerTypeResolver(typeof(MySpecialController)))

但是我一直试图使用 aspnetcore 2 获得等效的结果


创建该功能看起来很简单,因为您可以从默认值中派生出来ControllerFeatureProvider并覆盖IsController只识别您想要的控制器。

public class SingleControllerFeatureProvider : ControllerFeatureProvider {
    readonly Type m_type;

    public SingleControllerTypeResolver(Type type) => m_type = type;

    protected override bool IsController(TypeInfo typeInfo) {
       return base.IsController(typeInfo) && typeInfo == m_type.GetTypeInfo();
    }
}

下一部分是在启动过程中将默认提供程序替换为您自己的提供程序。

public void ConfigureServices(IServiceCollection services) {

    //...

    services
        .AddMvc()
        .ConfigureApplicationPartManager(apm => {
            var originals = apm.FeatureProviders.OfType<ControllerFeatureProvider>().ToList();
            foreach(var original in originals) {
                apm.FeatureProviders.Remove(original);
            }
            apm.FeatureProviders.Add(new SingleControllerFeatureProvider(typeof(MySpecialController)));
        });
        
    //...
}

如果重写默认实现被认为不够明确,那么您可以实现IApplicationFeatureProvider<ControllerFeature>直接并提供PopulateFeature你自己。

public class SingleControllerFeatureProvider 
    : IApplicationFeatureProvider<ControllerFeature> {
    readonly Type m_type;

    public SingleControllerTypeResolver(Type type) => m_type = type;
    
    public void PopulateFeature(
        IEnumerable<ApplicationPart> parts,
        ControllerFeature feature) {
        if(!feature.Controllers.Contains(m_type)) {
            feature.Controllers.Add(m_type);
        }
    }
}

参考ASP.NET Core 中的应用程序部分:应用程序功能提供程序 https://learn.microsoft.com/en-us/aspnet/core/mvc/advanced/app-parts?view=aspnetcore-2.0#application-feature-providers
参考发现 ASP.NET Core 中的通用控制器 https://stackoverflow.com/questions/36680933/discovering-generic-controllers-in-asp-net-core

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否有与旧 WebApi IHttpControllerTypeResolver 等效的 AspNetCore? 的相关文章

随机推荐