.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法

1.直接写在代码方式

Program.cs代码如下:

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进入发布目录输入命令:

dotnet myapi.dll

.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法-LMLPHP

2.命令行运行时动态输入IP与端口方式

Program.cs代码如下:

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进入发布目录输入命令:

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

.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法-LMLPHP

3.通过配置文件方式

Program.cs代码如下:

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"
}

.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法-LMLPHP

命令行cmd进入发布目录输入命令:

dotnet myapi.dll

.Net Core实战教程(二):设置Kestrel的IP与端口的几种方法-LMLPHP

精心准备,转载请留下原文地址。

05-11 20:14