【问题标题】:ASP.NET Web API Help Pages and VersioningASP.NET Web API 帮助页面和版本控制
【发布时间】:2014-10-11 12:27:17
【问题描述】:

我想为我的 API 的每个版本创建一个单独的帮助页面。例如,用户可以转到 /help?v=1 来查看 1.0 版路由,而 /help?v=2 可以查看 2.0 版路由。

使用SDammann.WebApi.Versioning,我向VersionedApiExplorer 添加了一个Version 属性,它将只返回定义版本的路由,并将版本作为参数添加到构造函数。然后我尝试了这个:

config.Services.Add(typeof(IApiExplorer), new VersionedApiExplorer(config, "1"));
config.Services.Add(typeof(IApiExplorer), new VersionedApiExplorer(config, "2"));

但这给了我以下错误:

The service type IApiExplorer is not supported.
Parameter name: serviceType

我只添加了一个服务实例 - config.Services.Replace(typeof(IApiExplorer), new VersionedApiExplorer(GlobalConfiguration.Configuration, "1")); - 以使配置正常工作,因此我可以测试我的帮助控制器。然后尝试了这个:

foreach (var service in Configuration.Services.GetServices(typeof(IApiExplorer))) {
    if (service.GetType() != typeof(VersionedApiExplorer)) continue;

    var explorer = service as VersionedApiExplorer;
    if (explorer.Version == v) {
        apiExplorer = explorer;
    }
}

这给出了我上面收到的相同错误。我知道我通常会使用this.Configuration.Services.GetApiExplorer(),但我不知道如何使用它来获取VersionedApiExplorer 的适当实例。我知道我可以直接在控制器中实例化适当的ApiExplorer,但如果可能的话,我希望将其保留在我的配置文件中。

所以我有两个问题:

  1. 如何将VersionedApiExplorer 类型的两个服务添加到我的配置对象?
  2. 如何在帮助控制器中检索适当的服务?

或者我可以采取完全不同的方法来实现相同的目标?

谢谢!

【问题讨论】:

  • 嗨,我想你从来没有解决过这个问题?我想做类似的事情。
  • 我有一个可行的解决方案,但我对它不是很满意。我基本上做了我在这个问题中描述的事情。我修改了VersionedApiExplorer 以接受版本号,并在我的HelpController 中设置适当的版本号。如果您觉得有帮助,我可以详细介绍。
  • 那太好了。也值得为未来的访问者回答您自己的问题。
  • 已发布。希望对您有所帮助。

标签: asp.net-web-api asp.net-web-api-helppages


【解决方案1】:

我最终选择了我在问题中暗示的解决方案。我觉得这个问题有更好的解决方案,但这可以完成工作。

首先,我在VersionedApiExplorer 中添加了一个Version 属性:

public string Version { get; private set; } 

然后我将InitializeApiDescriptions修改为如下所示:

private Collection<ApiDescription> InitializeApiDescriptions()
{
    Collection<ApiDescription> apiDescriptions = new Collection<ApiDescription>();
    var controllerSelector = configuration.Services.GetHttpControllerSelector();
    IDictionary<string, HttpControllerDescriptor> allControllerMappings = controllerSelector.GetControllerMapping();
    IDictionary<string, HttpControllerDescriptor> controllerMappings = new Dictionary<string, HttpControllerDescriptor>();
    // get only mappings for defined version
    if (allControllerMappings != null && Version != null) {
        foreach (var key in allControllerMappings.Keys) {
            if (key.Substring(0, key.IndexOf('.')) == VersionedControllerSelector.VersionPrefix + Version) {
                controllerMappings.Add(key, allControllerMappings[key]);
            }
        }
    }
    else if (Version == null) {
        controllerMappings = allControllerMappings;
    }

    if (controllerMappings != null)
    {
        foreach (var route in configuration.Routes)
            ExploreRouteControllers(controllerMappings, route, apiDescriptions);
    }
    return apiDescriptions;
}

我还添加了一个可以用来设置版本的方法:

public void SetVersion(string version) {
    this.Version = version;
    this.apiDescription = new Lazy<Collection<ApiDescription>>(InitializeApiDescriptions);
}

最后,我将HelpController 修改为如下所示:

public ActionResult Index(string v) {
    return this.View(GetApiExplorer(v).ApiDescriptions);
}

private IApiExplorer GetApiExplorer(string version) {
    if (version == null) {
        version = "1";
    }

    var apiExplorer = this.Configuration.Services.GetApiExplorer() as VersionedApiExplorer;
    if (apiExplorer != null) {
        apiExplorer.SetVersion(version);
    }

    return apiExplorer;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多