【问题标题】:Using XmlComment for swagger api helper page将 XmlComment 用于 swagger api 帮助页面
【发布时间】:2017-04-18 16:22:38
【问题描述】:

尝试为我的新启动 ASP.NET Core api 项目配置 swagger 时出错。它在本地主机上完美运行:评论与 xml 评论配合得很好。但是当我将它发布到 azure 主机时,它不起作用。

然后我试图找出方法是注释将xml注释添加到swagger的配置代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddMvcCore().AddApiExplorer();
    services.AddLogging();
    services.Configure<MvcOptions>(options =>
    {
        options.Filters.Add(new RequireHttpsAttribute());
    });

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info
        {
            Version = "v1",
            Title = " API Helper Page",
            Description = "A simple start ASP.NET Core Web API/ MBAAS",
            TermsOfService = "None",
            Contact = new Contact { Name = "Nguyễn Bá Nguyên", Email = "", Url = "https://github.com/hello/" },
            License = new License { Name = "Under Construction...", Url = " " }
        });
        // Set the comments path for the swagger json and ui.
        // only working on local, need to be fixed
        var basePath = PlatformServices.Default.Application.ApplicationBasePath;
        var xmlPath = Path.Combine(basePath, ".xml");
        c.IncludeXmlComments(xmlPath);
    });

}

为了在没有错误的情况下发布到天蓝色,我评论了最后一个

 //var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                //var xmlPath = Path.Combine(basePath, ".xml");
                //c.IncludeXmlComments(xmlPath

);

和天蓝色的主机工作,但招摇不能使用xml评论:(

那么有什么方法可以配置 swagger 以对 azure 主机使用 xml 注释?

【问题讨论】:

  • 尝试在你的项目中显式包含生成的 xml 文件,然后它应该包含在发布到 azure 步骤中,并且无需通过 kudu/FTP 手动上传即可使用。
  • @rory_za 你知道如何PlatformServices.Default.Application.ApplicationBasePath 找到xml注释文件(当我们配置时自动生成:解决方案资源管理器->属性->构建->输出->单击复选框XML 文档文件并选择输出路径)。那么我应该在哪里包含自动生成的生成的 xml 文件?
  • 将其设置为构建文件,然后至少构建一次项目,然后将生成的文件作为“现有项目”添加到您的项目中。

标签: c# azure asp.net-core swagger


【解决方案1】:

我在 Azure API 应用程序上使用带有 .NET 核心的 swagger xml cmets 遇到了同样的问题。在 web.config 中将 stdoutLogEnabled 标志设置为 true 后,我发现 XML 文件丢失了。在我手动将 xml 文件上传到 API 之后,它就可以工作了。

要上传 XML 文件,您可以使用 Kudu 服务(输入 &lt;yourapi&gt;.scm.azurewebsites.net 或在 Azure 门户的应用程序中 -> 开发工具 -> 高级工具)。然后单击调试控制台,导航到您的站点并上传 xml 文件:

【讨论】:

  • 实际上我不知道 PlatformServices.Default.Application.ApplicationBasePath 是如何找到 xml 注释文件(在我们配置时自动生成的:Solution explorer ->properties->build->output-> click to XML 文档文件的复选框并选择输出路径)
  • @MartinBrandl:当您可以设置要通过 project.json / csproj 发布的文件时,为什么还要手动上传? XDdocs.microsoft.com/en-us/dotnet/articles/core/tools/…
  • @Tseng 你是对的,这可能是要走的路。无论如何,他的问题的答案是缺少 XML 文件,因此 API 没有启动。
  • 我会更感兴趣,为什么他的代码首先在本地工作,因为他将基本路径与没有文件名的“.xml”组合在一起,这应该会产生像 X:\MyProj\Project.WebApi\bin\Debug\netcoreapp1.1\.xml 这样的路径而不是X:\MyProj\Project.WebApi\bin\Debug\netcoreapp1.1\Project.WebApi.xml
  • 因为我的 xml 文件是 ".xml" :))
猜你喜欢
  • 2016-08-21
  • 2014-12-27
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 2016-12-08
相关资源
最近更新 更多