【问题标题】:Web API Help Pages and API Explorer returning 0 descriptionsWeb API 帮助页面和 API Explorer 返回 0 个描述
【发布时间】:2015-10-06 15:18:00
【问题描述】:

我有这个项目,它只是一个 Web API 项目。在过去的某个时候,我删除了 HelpPages,并让应用程序使用了 OWIN。 现在我被要求添加我已经完成的 API HelpPages。 我已将我的 Startup 类设置为如下所示:

public void Configuration(IAppBuilder app)
{

    // Needs to be first
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    var httpConfig = new HttpConfiguration();

    // Register all areas
    AreaRegistration.RegisterAllAreas();

    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseWebApi(httpConfig);
}

这样我的帮助页面路径就可以正常工作了。 据我所知,这应该可以,但问题是 ApiExplorer 不会撤回任何描述。

在我的 ConfigureWebApi 方法中,我删除了格式,我已将其注释掉,但仍然无法正常工作,方法如下:

private void ConfigureWebApi(HttpConfiguration config)
{

    // Web API configuration and services
    var formatters = config.Formatters;
    var jsonFormatter = formatters.JsonFormatter;
    var serializerSettings = jsonFormatter.SerializerSettings;

    // Remove XML formatting
    formatters.Remove(config.Formatters.XmlFormatter);
    jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
    jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;

    // Configure our JSON output
    serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    serializerSettings.Formatting = Formatting.Indented;
    serializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    serializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

我实际上编辑了 HelpController 并在返回视图行放置了一个断点,这就是我知道 ApiExplorer 没有描述的方式:

public ActionResult Index()
{
    var docProdivder = Configuration.Services.GetDocumentationProvider();
    var desciptions = Configuration.Services.GetApiExplorer().ApiDescriptions;

    ViewBag.DocumentationProvider = docProdivder;
    return View(desciptions);
}

我在某处读到,如果我这样做:

public void Configuration(IAppBuilder app)
{

    // Needs to be first
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    var httpConfig = new HttpConfiguration();

    var exploerer = new ApiExplorer(httpConfig);
    var descriptions = exploerer.ApiDescriptions;

    // Register all areas
    AreaRegistration.RegisterAllAreas();

    ConfigureOAuthTokenGeneration(app);
    ConfigureOAuthTokenConsumption(app);
    ConfigureWebApi(httpConfig);
    app.UseWebApi(httpConfig);
}

我应该看到描述,但它仍然不起作用。 然后我在其他地方阅读以设置我的项目以输出 xml 描述文件并配置 HelpPageConfig 文件以使用 documentProvider。我生成了 Xml 描述文件,并且可以验证我的描述在那里,这是一个 sn-p:

    <member name="T:Melanite.Controllers.CollectionsController">
        <summary>
        Controller for all collection related functions
        </summary>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.#ctor">
        <summary>
        Default constructor
        </summary>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32)">
        <summary>
        Get all the collections for the given center
        </summary>
        <param name="centerId">The id of the center that the collection belongs to</param>
        <returns>A list of collections</returns>
    </member>
    <member name="M:Melanite.Controllers.CollectionsController.Get(System.Int32,System.DateTime)">
        <summary>
        Get all the collections for the given center on a specific date
        </summary>
        <param name="centerId">The id of the center that the collection belongs to</param>
        <param name="date">The planned collection date for the collections</param>
        <returns>A list of collections</returns>
    </member>

我像这样取消了 HelpPageConfig 中的行的注释:

// Uncomment the following to use the documentation from XML documentation file.
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

并确保 XML 文件位于 App_Data 文件夹中。名称都是正确的,但是当我运行我的项目时,我仍然没有从 ApiExplorer 中得到任何描述。

所以,正如你所见,我已经束手无策了。我希望有人以前遇到过这个问题并且知道如何解决它。如果你这样做,请帮助!

【问题讨论】:

  • 我相信我的问题与您的问题有关(我使用的是 Swashbuckle)。你找到解决办法了吗?这是我的,以防万一:stackoverflow.com/questions/31840165/…
  • 您好,感谢您的发帖,但我认为您的解决方案与我的不同。我的项目不使用 global.asax,如您所见,我正在调用 app.UseWebApi 方法。我暂时搁置了这个问题(正在处理另一个项目),但我会尽快处理它并进行更多测试以确认。

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


【解决方案1】:

我也有同样的问题。如果我添加了

GlobalConfiguration.Configure(WebApiConfig.Register)

在 Startup 类(我不使用 global.asax)中一切正常。我希望这对你也有帮助。

【讨论】:

  • 如果您无权访问 WebApiConfig.Register,而我的 Owin WebApi 项目没有,那么以下代码似乎对我有用。 GlobalConfiguration.Configure((config) => { config.MapHttpAttributeRoutes(); });
【解决方案2】:

如果您无权访问 WebApiConfig.Register(我在 Owin WebApi 项目中没有此权限),那么以下代码似乎对我有用。

GlobalConfiguration.Configure((config) => { config.MapHttpAttributeRoutes(); });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    相关资源
    最近更新 更多