2

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

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

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

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

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

4

1 回答 1

0

问题是 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;
于 2013-10-25T14:27:09.117 回答