Publishing web api with swagger on IIS

11,256

There is no problem with your Swagger settings. Please don’t forget configure the Swagger generator, as well as the comments path for the Swagger JSON.

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "ToDo API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("https://example.com/terms"),
                    Contact = new OpenApiContact
                    {
                        Name = "Shayne Boyer",
                        Email = string.Empty,
                        Url = new Uri("https://twitter.com/spboyer"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under LICX",
                        Url = new Uri("https://example.com/license"),
                    }
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });
        }

Besides, Please ensure that the server has installed the Asp.net core hosting bundle on the sever-side.
https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.6-windows-hosting-bundle-installer
Feel free to let me know if there is anything I can help with.

Share:
11,256
doorman
Author by

doorman

Updated on July 10, 2022

Comments

  • doorman
    doorman almost 2 years

    I am trying figure out how to publish a .net core 3 API with Swagger (SwashBuckle) after following this example documentation . So it works locally and when I hit F5 IIS Express launches the site under http://localhost:8033/index.html

    Here is the Configure code in startup.cs:

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
    
                app.UseStaticFiles(new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(env.ContentRootPath),
                    RequestPath = new PathString("")
                });
    
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
                    c.DocumentTitle = "TestAPI";
                    c.DocExpansion(DocExpansion.None);
                    c.RoutePrefix = string.Empty;                   
                });                
            }
    

    Next I published the API to a local folder and copied the files to the IIS folder on the server. If I open the server API domain I get a page can’t be found. Which address should I use to open up the swagger UI on the server? Is something missing from the configuration?

  • doorman
    doorman almost 4 years
    Hi @AbrahamQian thanks for your explanation. I have all the code in place and the API is working that is I am able to trigger a REST call from a client. However the SwaggerUI isn´t shown on the server. I assume it should be enough to point to the index.html like this MyServerAPI/index.html ? Even though there are no static files or wwwroot folder located in the project I assume the Swagger will generate this on the fly?
  • Abraham Qian
    Abraham Qian almost 4 years
    yes, the URL is enough to navigate the SwaggerUI. But I am not sure about the second question. You could try to configure in the CSPROJ file to allow the compiler to generate a file , just like the document said.