Deploy an ASP.NET Core-Web-API (.NET 5, Visual Studio 2019) to IIS

Matthias Schuppe 21 Reputation points
2022-03-26T09:15:17.397+00:00

Dear all,

I'm trying desperately to deploy an ASP.NET Core-Web-API (.NET 5) to IIS. I use Visual Studio 2019.

To test server setup (ASP.NET Core Hosting Bundle 5.0.15) I created an ASP.NET Core-Web-App and deployed it via file system, it works.

Then I created an ASP.NET Core-Web-API, the project template ("WeatherForecast") directly works locally in IIS Express (swagger page appears). A direct deploy to IIS without changes leads to HTTP 404.

Then I had a long web search and tried many possibilities, see below, without success. It can't be impossible to deploy an ASP.NET Core-Web-API to IIS, can you help me? Thank you very much!

This is what I tried:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()  // I tried also UseIIS instead of
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}


public static void Main(string[] args)
{
    var host = new HostBuilder()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureWebHost(webBuilder =>
        {
            webBuilder.UseKestrel(serverOptions =>
            {
            })
            .UseIISIntegration()
            .UseStartup<Startup>();
        })
        .Build();

    host.Run();
}


public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseKestrel();  // I tried also UseIIS instead of
            webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
            webBuilder.UseIISIntegration();
            webBuilder.UseStartup<Startup>();
        });


public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureKestrel(serverOptions =>
            {
            })
            .UseStartup<Startup>();
        });
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
0 comments No comments
{count} votes

Accepted answer
  1. satya karki 986 Reputation points MVP
    2022-03-26T09:49:33.597+00:00

    Hi @Matthias Schuppe

    You can follow below article for publishing asp.net core 5 Web API into IIS.

    https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-5.0&tabs=visual-studio

    Would you please share how have you configure swagger?

    Please check if your swagger in production or dev environment.

    if (env.IsDevelopment()) // it run only in dev not in IIS  
                {  
                    app.UseDeveloperExceptionPage();  
                    //app.UseSwagger();  
                    //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));  
                }  
    
    
      
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Matthias Schuppe 21 Reputation points
    2022-03-27T08:17:05.01+00:00

    Hello @satya karki

    Thank you very much, you brought me to the solution. Sometimes I checked only the default page and I expected swagger here. Now it's okay.

    0 comments No comments