ASP.NET Web API 单独项目下的帮助页面

ASP.NET Web API Help page under separate project

我有 ASP.NET Web API 项目,我想添加一个帮助页面,但我希望它位于一个单独的项目中。

可能吗?

您可以将 XmlDocumentationProvider 构造函数重写为类似的东西:

public XmlDocumentationProvider(string appDataPath)
{
    if (appDataPath == null)
    {
        throw new ArgumentNullException(nameof(appDataPath));
    }

    var files = new[] { "MyWebApiProject.xml" /*, ... any other projects */ };
    foreach (var file in files)
    {
        var xpath = new XPathDocument(Path.Combine(appDataPath, file));
        _documentNavigators.Add(xpath.CreateNavigator());
    }
}

它将包括您在数组中列出的所有 xml 文档文件。您还应该确保您的目标 Web API 项目(以及所有模型项目,如果需要)生成构建文档并将其复制到正确的位置。

您应该从您的帮助项目中的 Web API 项目调用 WebApiConfig.Register:

  protected void Application_Start()
  {
     AreaRegistration.RegisterAllAreas();
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     GlobalConfiguration.Configure(MyApiProjectNamespace.WebApiConfig.Register);
  }