You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署于IIS的ASP.NET Core MVC应用Program类应如何编写?

How to Add UseIISIntegration and Configure Kestrel in Your ASP.NET Core Program Class

Hey there! Let's walk through how to update your Program class to include UseIISIntegration and explicitly work with Kestrel (it’s worth noting that WebHost.CreateDefaultBuilder already sets up Kestrel by default, but making it explicit helps with clarity or customizations later).

Here's the Updated Code

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            // Explicitly configure Kestrel (optional but useful for custom settings)
            .UseKestrel(options =>
            {
                // Add custom Kestrel configurations here if needed, e.g.:
                // options.Listen(IPAddress.Loopback, 5000); // HTTP port
                // options.Listen(IPAddress.Loopback, 5001, listenOptions =>
                // {
                //     listenOptions.UseHttps("your-certificate.pfx", "cert-password");
                // });
            })
            // Enable IIS Integration for hosting with IIS/IIS Express
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
    }
}

Key Details to Know:

  • UseKestrel(): This explicitly enables Kestrel, ASP.NET Core's default web server. Even though CreateDefaultBuilder includes this under the hood, calling it directly lets you tweak settings like port numbers, HTTPS certificates, or connection limits inside the lambda.
  • UseIISIntegration(): This sets up the reverse proxy integration between Kestrel and IIS/IIS Express. IIS handles process management, authentication, and request forwarding, while Kestrel processes the actual web requests—this is critical if you’re deploying to IIS or using IIS Express locally.
  • Default Behavior: If you skip explicitly calling UseKestrel(), the default builder still configures it with standard settings. Adding it just makes your configuration more transparent, especially if you need to adjust Kestrel’s behavior down the line.

内容的提问来源于stack exchange,提问作者ProfK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:48:06