部署于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 thoughCreateDefaultBuilderincludes 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
相关产品推荐
相关产品推荐

