DotNETCore 学习笔记 宿主

Hosting
--------------------------------------------------------------------------
Setting up a Host :
using Microsoft.AspNetCore.Hosting;
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }


var host = new WebHostBuilder()
    .UseKestrel()
    .Configure(app =>
    {
        app.Run(async (context) => await context.Response.WriteAsync("Hi!"));
    })
    .Build();

host.Run();
--------------------------------------------------------------------------
Configuring a Host:

new WebHostBuilder()
    .UseSetting("applicationName", "MyApp")

Host Configuration Values

Application Name string
Key: applicationName. This configuration setting specifies the value that will be returned from 

IHostingEnvironment.ApplicationName.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Capture Startup Errors bool
Key: captureStartupErrors. Defaults to false. When false, errors during startup result in the host exiting. 

When true, the host will capture any exceptions from the Startup class and attempt to start the server. It 

will display an error page (generic, or detailed, based on the Detailed Errors setting, below) for every 

request. Set using the CaptureStartupErrors method.

new WebHostBuilder()
    .CaptureStartupErrors(true)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Content Root string
Key: contentRoot. Defaults to the folder where the application assembly resides (for Kestrel; IIS will use 

the web project root by default). This setting determines where ASP.NET Core will begin searching for 

content files, such as MVC Views. Also used as the base path for the Web Root setting. Set using the 

UseContentRoot method. Path must exist, or host will fail to start.

new WebHostBuilder()
    .UseContentRoot("c:\\mywebsite")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Detailed Errors bool
Key: detailedErrors. Defaults to false. When true (or when Environment is set to “Development”), the app 

will display details of startup exceptions, instead of just a generic error page. Set using UseSetting.

new WebHostBuilder()
    .UseSetting("detailedErrors", "true")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Environment string
Key: environment. Defaults to “Production”. May be set to any value. Framework-defined values include 

“Development”, “Staging”, and “Production”. Values are not case sensitive. See Working with Multiple 

Environments. Set using the UseEnvironment method.

new WebHostBuilder()
    .UseEnvironment("Development")
++++++++++++++++++++++++++++++++++++++++++++++++++
Server URLs string
new WebHostBuilder()
    .UseUrls("http://*:5000;http://localhost:5001;https://hostname:5002")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Startup Assembly string
new WebHostBuilder()
    .UseStartup("StartupAssemblyName")
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Web Root string
new WebHostBuilder()
    .UseWebRoot("public")

public static void Main(string[] args)
{
  var config = new ConfigurationBuilder()
    .AddCommandLine(args)
    .AddJsonFile("hosting.json", optional: true)
    .Build();

  var host = new WebHostBuilder()
    .UseConfiguration(config)
    .UseKestrel()
    .Configure(app =>
    {
      app.Run(async (context) => await context.Response.WriteAsync("Hi!"));
    })
  .Build();

  host.Run();
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dotnet run --urls "http://*:5000"
var urls = new List<string>() {
  "http://*:5000",
  "http://localhost:5001"
  };
var host = new WebHostBuilder()
  .UseKestrel()
  .UseStartup<Startup>()
  .Start(urls.ToArray());

using (host)
{
  Console.ReadLine();
}

 

转载于:https://www.cnblogs.com/ziranquliu/p/5872233.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值