我的MVC核心应用程序的Program类在IIS中托管应该是什么样的?

时间:2018-04-12 08:18:36

标签: asp.net asp.net-core asp.net-core-mvc asp.net-core-2.0

目前,Program类是默认的,由MVC应用程序项目模板创建,如下所示:

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

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

我知道我应该在那里的某个地方进行UseIISIntegration调用,Kestrel应该以某种方式参与,但我只知道这一点。

有人可以给我一个示例,说明Program类的内容应该按顺序查看,而不是我的MVC应用程序已正确配置为在IIS上托管。 MS Docs Host ASP.NET Core on Windows with IIS主题只有这个不太简单的例子:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        ...

1 个答案:

答案 0 :(得分:1)

在Asp.Net Core 1.x中,因为您习惯于在启用IISIntegration组件时使用

var host = new WebHostBuilder()
    .UseKestrel()
    .UseIISIntegration()
    ...

因为UseKestrelUseIISIntegration都是必需的。

在最近的版本中,Asp.Net Core 2.x已经在WebHost.CreateDefaultBuilder方法中合并了

  

以下默认值应用于返回的WebHostBuilder:使用Kestrel作为Web服务器,将ContentRootPath设置为GetCurrentDirectory()的结果,从&quot; appsettings.json&#39;加载IConfiguration。和[&#39; appsettings。[EnvironmentName] .json&#39;,当EnvironmentName为&#39;开发&#39;时,从用户秘密加载IConfiguration。使用条目程序集,从环境变量加载IConfiguration,配置ILoggerFactory以登录到控制台和调试输出,启用IIS集成,使框架能够将其选项绑定到其默认配置节,并在EnvironmentName时添加开发人员异常页面是&#39;发展&#39;

再次引用文档

  

典型的 Program.cs 调用CreateDefaultBuilder开始设置主机。 CreateDefaultBuilder Kestrel 配置为Web服务器,并通过配置ASP.NET核心模块的基本路径和端口来启用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()
    ...

使用WebHostBuilder仍然是ASP.NET Core 2.x支持的方法。

参考Host ASP.NET Core on Windows with IIS

有关在Asp.Net Core中托管的更多信息

参考Hosting in ASP.NET Core