溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

發(fā)布時(shí)間:2021-01-22 16:19:35 來源:億速云 閱讀:192 作者:Leah 欄目:開發(fā)技術(shù)

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

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

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

發(fā)布如下:

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

一:解決辦法1(UseUrls)

       骨架代碼就那么幾行,很容易在這個(gè)IWebHostBuilder中找到一個(gè)叫做UseUrls的方法,從注解中可以看得出來讓W(xué)ebHost監(jiān)聽指定的端口號(hào),截圖如下:

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

那么答案就出來了,你需要自己來指定一下端口,改完之后截圖如下:

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

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

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

但是在發(fā)布之后,你突然發(fā)現(xiàn),臥槽,端口沖突了,我想換端口,tmd我還得為此再發(fā)一次程序,一個(gè)字麻煩,說一送一。差點(diǎn)被砍到的第一反應(yīng)就是把硬編碼

送到配置文件中。

二:解決辦法2 (host.json)

       你會(huì)突然發(fā)現(xiàn)要使用到的Configuration屬性只能在Startup類中,畢竟在WebHost的Build之前ServiceCollection都沒有初始化,哪里有統(tǒng)一化的配置系統(tǒng)呢,

 那怎么辦,還能怎么辦,自己定義一個(gè)Configuration了,然后修改的步驟如下:

1. 新增一個(gè)host.json,名字隨便定義,自己看得懂就行啦。

{
 "url": "http://*:9099"
}

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

2. webhost代碼修改

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
 {
  var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
      .AddJsonFile("host.json")
      .Build();

  var url = configuration["url"];

  return WebHost.CreateDefaultBuilder(args).UseUrls(configuration["url"])
       .UseStartup<Startup>();
 }

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

      問題倒是解決了,但是總發(fā)現(xiàn)有一點(diǎn)不爽,突然新來的Configration就好像半路殺出的陳咬金,所以說如果將陳咬金收編過來就完美了。

三:不夠優(yōu)雅后的整合

      接下來你很容易會(huì)在WebHostBuilder中發(fā)現(xiàn)另一個(gè)方法UseConfiguration,看參數(shù)就是用來接收ConfigurationRoot的,所以就把代碼修改如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
 {
  var configuration = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
      .AddJsonFile("host.json")
      .Build();

  //var url = configuration["url"];

  return WebHost.CreateDefaultBuilder(args).UseConfiguration(configuration)
       .UseStartup<Startup>();
 }

        但是這里有一個(gè)問題,asp.netcore能識(shí)別我自定義的url嗎?肯定是不能識(shí)別的啦,那問題就是,AspnetCore模式會(huì)用哪一個(gè)key作為url的地址呢??

要找到答案的話得需要從源碼上啦,從UseUrls入手吧。

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

從上面可以看到,UseUrls默認(rèn)是使用 WebHostDefaults.ServerUrlsKey 作為url的key的,然后繼續(xù)F12看一下它的 內(nèi)容是什么?

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

   好了,真想大白了,原來是urls,接下來我只需要把host.json 的url改成urls就可以了,對(duì)吧。

{
 "urls": "http://*:9099"
}

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

 四:解決辦法3 (使用docker)

      如果你不想做出任何改變,不想做任何退步,那沒辦法,只能把你關(guān)進(jìn)docker里啦。

1. dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime

MAINTAINER hxc@qq.com

RUN mkdir /data

COPY ./publish/ /data

WORKDIR /data

CMD [ "dotnet","WebApplication1.dll" ]

2. publish 文件夾

   在dockerfile的同級(jí)目錄下,新建一個(gè)publish文件夾用來存放當(dāng)前dll文件。

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

3. 通過build從dockerfile中構(gòu)建鏡像

[root@localhost tsweb]# docker build --rm -f ts.dockerfile -t a/netcore:v1 .
Sending build context to Docker daemon 2.56 kB
Step 1/6 : FROM microsoft/dotnet:2.1-sdk
 ---> bde01d9ed6eb
Step 2/6 : MAINTAINER hxc@qq.com
 ---> Using cache
 ---> 3af0c3f7c416
Step 3/6 : RUN mkdir /data
 ---> Using cache
 ---> 97137ffc5449
Step 4/6 : COPY ./publish/ /data
 ---> Using cache
 ---> 77a94f1a0b8f
Step 5/6 : WORKDIR /data
 ---> Using cache
 ---> 6778c2054a7b
Step 6/6 : CMD dotnet WebApplication1.dll 
 ---> Running in e4a69b32e702
 ---> 9ed3a9769610
Removing intermediate container e4a69b32e702
Successfully built 9ed3a9769610

4. 最后啟動(dòng)鏡像,用8888綁定到默認(rèn)的5000端口

[root@localhost tsweb]# docker run -d -p 8888:5000 --name a-webcore-v1 a/netcore:v1
f94c727b98d5654aa560308752c2af7cde550b6cc06c520bd438e4ccf1fa616d

5. 然后你清楚的看到8888端口已經(jīng)打開了,但是卻不能訪問,尷尬。。。

[root@localhost tsweb]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State PID/Program name 
tcp 0 0 192.168.122.1:53 0.0.0.0:*  LISTEN 1834/dnsmasq 
tcp 0 0 0.0.0.0:22  0.0.0.0:*  LISTEN 1135/sshd  
tcp 0 0 127.0.0.1:631  0.0.0.0:*  LISTEN 1136/cupsd  
tcp 0 0 127.0.0.1:25  0.0.0.0:*  LISTEN 1582/master  
tcp6 0 0 :::3306   :::*   LISTEN 2451/mysqld  
tcp6 0 0 :::22   :::*   LISTEN 1135/sshd  
tcp6 0 0 ::1:631   :::*   LISTEN 1136/cupsd  
tcp6 0 0 :::8888   :::*   LISTEN 9531/docker-proxy-c 
tcp6 0 0 ::1:25   :::*   LISTEN 1582/master  
[root@localhost tsweb]#

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

6. 解決這個(gè)問題的第一步就要看一下 容器中真的開放出來了5000端口嗎,可通過docker logs 或 docker ps 查看

[root@localhost tsweb]# docker logs b-webcore-v1
Hosting environment: Production
Content root path: /data
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
[root@localhost tsweb]#

  原來開放的是80端口哈~~~~  那就簡(jiǎn)單了,把原來的容器給刪了,重新生成一個(gè)容器再映射一下就好啦。

[root@localhost tsweb]# docker rm -f b-webcore-v1
b-webcore-v1
[root@localhost tsweb]# docker run -d -p 8888:80 --name b-webcore-v1 b/netcore:v1
e58039e02740e37cc431c1176fbf586ab19b02bd9331040e4719e9d46e51627d
[root@localhost tsweb]#

怎么在asp.net core中對(duì)默認(rèn)端口進(jìn)行修改

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI