c# - Swagger UI 未在 azure .net core 中生成,但它在本地工作

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

在 Azure 中部署时,Swagger UI 不会在 .net core 应用程序中创建,但它在本地运行良好

我已将其添加到

startup.cs中的ConfigureServices(IServiceCollection services)方法

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "API",
Description = "API"
});
});

app.UseSwagger();
app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
  c.RoutePrefix = "swagger";
});
app.UseAuthentication();

env.ConfigureNLog("nlog.config");
loggerFactory.AddNLog();

app.UseSignalR(routes =>
{
  routes.MapHub<DashboardHub>("/hubs/dashboard");
});

app.UseMvc(routes =>
{
   routes.MapRoute(
   name: "default",
   template: "{controller}/{action=Index}/{id?}");
});

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
         spa.UseReactDevelopmentServer(npmScript: "start");
    }
});

在配置中(IApplicationBuilder 应用程序,IHostingEnvironment env,ILoggerFactory loggerFactory)

最佳答案

确保代码不是 IF 条件 (env.IsDevelopment())。 这就是为什么只能在本地主机上工作的原因。这避免了在生产时间工作。

像示例一样去掉swagger初始化。

            if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();

            // NOT HERE

        }

        // Enable middleware to serve generated Swagger as a JSON endpoint.  
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),  
        // specifying the Swagger JSON endpoint.  
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });

关于c# - Swagger UI 未在 azure .net core 中生成,但它在本地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57674083/

相关文章:

c# - 如何防止 ASP.net core 中的默认 url 解码

c# - 将 DbSet<TEntity> 属性移动到 Entity Framework Core 中的单独类

c# - lambda 函数是否可以包含 Razor 语法并在 View 中执行?

c# - EntityFramework 未识别手动进行的实体更改

azure - 有没有办法从 Azure 中的服务总线队列中删除待处理的消息?

连接到数据库的 Azure Function App 无法正常工作

azure - Azure 上的 Axe - 与本地客户端的连接

c# - 使用多线程在 SQLite 数据库中插入记录的性能问题

c# - 如何在 C# 中创建对象的 Array 或 ArrayList?

azure - 无法让嵌套的 StartOperation 显示在筛选器下拉列表中