[英]What should my MVC Core app's Program class look like for hosting in IIS?
At the moment, the Program
class is default, as created by the MVC Application project template, and looks like this: 目前, Program
类是MVC Application项目模板创建的默认类,如下所示:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
I know that I should also have a UseIISIntegration
call somewhere in there, and that Kestrel
should be somehow involved, but I know nothing more than that. 我知道我也应该在那里有一个UseIISIntegration
调用,并且应该以某种方式参与Kestrel
,但是我仅此而已。
Could somebody please give me an example on what the content of the Program
class should look in order than my MVC app is properly configured for hosting on IIS. 有人可以给我一个例子,说明我的MVC应用程序已正确配置为在IIS上托管时, Program
类的内容应按什么顺序排列。 The MS Docs Host ASP.NET Core on Windows with IIS topic only has this less than meager example: Windows上带有IIS的Windows上的MS Docs Host ASP.NET Core主题仅具有以下不足的示例:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
...
In Asp.Net Core 1.x as you are accustom to when enabling the IISIntegration components you would have done 习惯于在Asp.Net Core 1.x中启用IISIntegration组件时
var host = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
...
as both UseKestrel
and UseIISIntegration
are required. 因为同时需要UseKestrel
和UseIISIntegration
。
In the more recent version, Asp.Net Core 2.x, that has been combined inside the WebHost.CreateDefaultBuilder
method 在最新版本的Asp.Net Core 2.x中,已将其合并到WebHost.CreateDefaultBuilder
方法中
The following defaults are applied to the returned WebHostBuilder: use Kestrel as the web server, set the ContentRootPath to the result of GetCurrentDirectory(), load IConfiguration from 'appsettings.json' and 'appsettings.[EnvironmentName].json', load IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly, load IConfiguration from environment variables, configures the ILoggerFactory to log to the console and debug output, enables IIS integration, enables the ability for frameworks to bind their options to their default configuration sections, and adds the developer exception page when EnvironmentName is 'Development' 以下默认值应用于返回的WebHostBuilder:使用Kestrel作为Web服务器,将ContentRootPath设置为GetCurrentDirectory()的结果,从“ appsettings.json”和“ appsettings。[EnvironmentName] .json”加载IConfiguration,从使用入口程序集将EnvironmentName设为“开发”,从环境变量加载IConfiguration,将ILoggerFactory配置为登录到控制台并调试输出,启用IIS集成,使框架能够将其选项绑定到其默认配置部分的功能时,用户的秘密并在EnvironmentName为'Development'时添加开发人员例外页面
To quote the documentation again 再次引用文档
A typical Program.cs calls
CreateDefaultBuilder
to begin setting up a host. 典型的Program.cs调用CreateDefaultBuilder
来开始设置主机。CreateDefaultBuilder
configures Kestrel as the web server and enables IIS integration by configuring the base path and port for the ASP.NET Core ModuleCreateDefaultBuilder
将Kestrel配置为Web服务器,并通过配置ASP.NET Core模块的基本路径和端口来启用IIS集成。
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
...
In Asp.Net Core 1.x as you are accustom to when enabling the IISIntegration components you would have done
var host = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
...
Using the WebHostBuilder
is still a supported approach with ASP.NET Core 2.x. ASP.NET Core 2.x仍然支持使用WebHostBuilder
。
Reference Host ASP.NET Core on Windows with IIS Windows上具有IIS的参考主机ASP.NET Core
For more information on hosting in Asp.Net Core 有关在Asp.Net Core中托管的更多信息
Reference Hosting in ASP.NET Core ASP.NET Core中的参考托管
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.