webapi 版本控制控制器名称获取

2019-01-02 23:35发布

webapi项目需要进行版本控制。我继承了类IHttpControllerSelector并在SelectController方法需要获取控制器名称。因为项目存在特性路由,请问该如何获取呢

1条回答
Emotional °昔
2楼-- · 2019-01-03 00:22

直接给出代码,供参考。其中ApiGroups里面的keys就是controller的名字

private void LoadAssemblies()
        {
            var configuration = new HttpConfiguration(new HttpRouteCollection());
            IAssembliesResolver assembliesResolver = new AnotherAssemblyResolver();
            configuration.Services.Replace(typeof(IAssembliesResolver), assembliesResolver);

            configuration.MapHttpAttributeRoutes();
            configuration.EnsureInitialized();
            var apiExplorer = new ApiExplorer(configuration);
            var descriptions = apiExplorer.ApiDescriptions;
            ApiGroups = descriptions.Any() ?
                descriptions
                .GroupBy(d => d.ActionDescriptor.ControllerDescriptor.ControllerName)
                .ToDictionary(para => para.Key, para => para.ToList()) : new Dictionary<string, List<ApiDescription>>();
        }
查看更多
登录 后发表回答