.Net Core實戰教程(二):設置Kestrel的IP與端口的幾種方法

.Net Core實戰教程(二):設置Kestrel的IP與端口的幾種方法

1.直接寫在代碼方式

Program.cs代碼以下:json

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace myapi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://192.168.3.162:6600")
                .UseStartup<Startup>();
    }
}

 命令行cmd進入發佈目錄輸入命令:api

dotnet myapi.dll

2.命令行運行時動態輸入IP與端口方式

Program.cs代碼以下:ui

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace myapi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build())
                .UseStartup<Startup>();
    }
}

命令行cmd進入發佈目錄輸入命令:url

dotnet myapi.dll --server.urls "http://192.168.3.185:7100"

3.經過配置文件方式

Program.cs代碼以下:spa

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace myapi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseConfiguration(new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("hosting.json", optional: true).Build())
                .UseStartup<Startup>();
    }
}

項目添加"hosting.json "文件,代碼以下:命令行

{
  "server.urls": "http://192.168.3.185:8800"
}

命令行cmd進入發佈目錄輸入命令:3d

dotnet myapi.dll

 

 

精心準備,轉載請留下原文地址。code

相關文章
相關標籤/搜索