ASP .Net Core 2.0 修改默认端口

ASP .Net Core 的默认端口是5000,如果想在同一台服务器上运行多个实例,就不能都监听5000端口了,需要每一个实例都监听不同的端口。当然,如果您正在使用IIS或者Jexus来托管,可以不用特意修改端口即可正常运行多个实例。

方式一

第一种方式是直接修改修改程序,在初始化Kestrel Server的时候指定端口:

namespace ZKEACMS.WebHost
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup()
                .UseUrls("http://*:5123") //直接指定端口
                .Build();

            host.Run();
        }
    }
}

直接写死在程序里的这种做法显然不是推荐的,不方便使用。

方式二

可以通过设置环境变量(ASPNETCORE_URLS)的方式来修改.Net Core的默认端口(5000)。

开发环境
# Unix:
ASPNETCORE_URLS="http://*:5123" dotnet run

# Windows PowerShell:
$env:ASPNETCORE_URLS="http://*:5123" ; dotnet run

# Windows CMD (note: no quotes):
SET ASPNETCORE_URLS=http://*:5123 && dotnet run
Visual Studio

 

生产环境
# Unix:
ASPNETCORE_URLS="http://*:5123" dotnet application.dll

# Windows PowerShell:
$env:ASPNETCORE_URLS="http://*:5123" ; dotnet application.dll

# Windows CMD (note: no quotes):
SET ASPNETCORE_URLS=http://*:5123 && dotnet application.dll
Linux的Unit配置

增加一个Environment配置即可。Environment=ASPNETCORE_URLS=http://*:5123

[Unit]
Description=ZKEACMS

[Service]
WorkingDirectory=/root/cms
ExecStart=/usr/bin/dotnet /root/cms/ZKEACMS.WebHost.dll
Restart=always
RestartSec=10
SyslogIdentifier=zkeacms
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=ASPNETCORE_URLS=http://*:5123

[Install]
WantedBy=multi-user.target

原文地址:http://www.zkea.net/codesnippet/detail/post-83

转载于:https://www.cnblogs.com/seriawei/p/8303351.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值