【问题标题】:Can no longer access the list of API descriptors after installing Glimpse安装 Glimpse 后无法再访问 API 描述符列表
【发布时间】:2013-05-27 07:06:02
【问题描述】:

我们想在我们的 ASP.net MVC 项目中使用 Glimpse,但在访问 Web API 描述符列表时遇到了问题。

我们已经使用 Nuget 包管理器安装了 Glimpse.Mvc4 1.2.2。

以下 sn-p 获取所有 API 描述符,在我们安装 Glimpse 之前它可以正常工作。安装后我们只会得到一个空列表。

IEnumerable<ApiDescription> apiDescriptors = System.Web.Http.GlobalConfiguration
           .Configuration
           .Services
           .GetApiExplorer()
           .ApiDescriptions

有谁知道为什么安装 Glimpse 后这个调用不起作用?

【问题讨论】:

  • 这听起来可能是一个错误。 Glimpse 没有做任何可能干扰 WebAPI 的具体操作,但该调用中可能会发生一些奇怪的事情。请随时open an issue on GitHub,以便我们追踪并修复它。
  • 这也发生在我身上,我在一个干净的项目中重现了它。当我通过 NuGet 卸载 Glimpse 时,API 又回来了。问题出在 ApiExplorer 上,它查看您的控制器和路由来为您的 API 构建元数据。 Glimpse 项目是否存在问题(我没有看到)?
  • 几周前我确实在 Glimpse 项目上打开了一个问题:github.com/Glimpse/Glimpse/issues/365 不过我还没有得到任何回复。

标签: asp.net-mvc-4 asp.net-web-api glimpse


【解决方案1】:

问题在于 Glimpse ASP.NET 模块将所有路由包装在它自己的对象中。所以 API Explorer 会忽略它们(认为它们不是 Web API 路由)。

要解决此问题,您必须初始化 System.Web.Http.HttpConfiguration 的新副本,而不是使用 GlobalConfiguration.Configuration

// need to create a custom configuration instance because the default one can be 
// processed by Glimpse and be thus full of wrapped routesthat the ApiExplorer 
// does not recognize.
var config = new System.Web.Http.HttpConfiguration();
WebApiConfig.Register(config);

// these line is useful if you use WebAPI Help page package
HelpPageConfig.Register(config);
Controllers.HelpController.Configuration = config;

// otherwise just get the `IApiExplorer` interface from the fresh configuration:
var apis = config.Services.GetApiExplorer().ApiDescriptions;

【讨论】:

    猜你喜欢
    • 2017-12-20
    • 2016-04-04
    • 2017-06-19
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 2012-11-06
    相关资源
    最近更新 更多