Is it possible to self-host a ASP.NET Core Application without IIS?

前端 未结 3 1345
Happy的楠姐
Happy的楠姐 2020-11-29 23:36

I have a full-working ASP.NET MVC application (.NET Core, ASP.NET Core) which runs fine in Visual Studio (which uses IISExpress).

I would now like to have a console

3条回答
  •  囚心锁ツ
    2020-11-30 00:40

    Is it possible to self-host an ASP.NET Core Application without IIS?

    Yes. In fact, all ASP.NET Core applications are self-hosted. Even in production, IIS/Nginx/Apache are a reverse proxy for the self-hosted application.

    In a reasonably standard Program.cs class, you can see the self-hosting. The IISIntegration is optional - it's only necessary if you want to integrate with IIS.

    public class Program
    {
        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .AddCommandLine(args)
                .AddEnvironmentVariables(prefix: "ASPNETCORE_")
                .Build();
    
            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup()
                .Build();
    
            host.Run();
        }
    }
    

提交回复
热议问题