In general, the default port after Aspnetcore is 5000, which everyone knows, and the default skeleton code doesn't see any IP address and port number that you enter, but as programmers we don't want
is controlled by the framework, how to implement the default port modification?
Skeleton Code:
Public class program { publicstaticvoid Main (string[] args) { Createwebhostbuilder (args). Build (). Run (); } Public Static Iwebhostbuilder Createwebhostbuilder (string[] args) = webhost.createdefaultbuilder (args) . Usestartup<Startup>(); }
Published as follows:
One: Solution 1 (useurls)
Skeleton code just a few lines, it is easy to find a method called Useurls in this iwebhostbuilder, from the annotations can be seen to let webhost listen to the specified port number, as follows:
Then the answer will come out, you need to specify the port yourself, after changing the following:
Public classProgram { Public Static voidMain (string[] args) {Createwebhostbuilder (args). Build (). Run (); } Public StaticIwebhostbuilder Createwebhostbuilder (string[] args) =Webhost.createdefaultbuilder (args) . Useurls ( "http://*:8080") . Usestartup<Startup>(); }
But after the release, you suddenly found that the slot, port conflict, I want to change the port, TMD I have to send a program again, a word trouble, say one get one. The first reaction that was almost cut was to put hard-coded
Sent to the configuration file.
II: Solution 2 (Host.json)
You'll suddenly find that the configuration property you want to use can only be in the startup class, after all, servicecollection is not initialized before the webhost build, where there is a unified configuration system,
What to do, what else to do, you define a configuration, and then modify the following steps:
1. Add a Host.json, the name casually defined, you can understand it.
{ "url""http://*:9099"}
2. Webhost Code Modification
Public StaticIwebhostbuilder Createwebhostbuilder (string[] args) { varConfiguration =NewConfigurationbuilder (). Setbasepath (environment.currentdirectory). Addjsonfile ("Host.json") . Build (); varURL = configuration["URL"]; returnWebhost.createdefaultbuilder (args). Useurls (configuration["URL"]) . Usestartup<Startup>(); }
The problem is solved, but always found a little uncomfortable, suddenly the new configration is like halfway to kill the Chen Bite gold, so that if the Chen Bite Gold incorporated over the perfect.
Three: the integration after the lack of elegance
Next you can easily find another method in Webhostbuilder useconfiguration, see the parameter is used to receive configurationroot, so the code is modified as follows:
Public StaticIwebhostbuilder Createwebhostbuilder (string[] args) { varConfiguration =NewConfigurationbuilder (). Setbasepath (environment.currentdirectory). Addjsonfile ("Host.json") . Build (); //var url = configuration["url"]; returnWebhost.createdefaultbuilder (args). useconfiguration (configuration) . Usestartup<Startup>(); }
But here's a question, asp.netcore can you identify my custom URL? Must not be recognized, the question is, Aspnetcore mode will use which key as the address of the URL??
To find the answer to the words need to from the source, from the Useurls start.
As can be seen from the above, Useurls defaults to using Webhostdefaults.serverurlskey as the URL key, and then continue F12 to see what it is?
Well, I really want to find out, the original is URLs, then I just need to change the URL of Host.json to URLs on it, right.
{ "URLs""http://*:9099"}
IV: Workaround 3 (using Docker)
If you don't want to make any changes and don't want to do anything backwards, there's no way to put you in Docker.
1. Dockerfile
from microsoft/dotnet:2.1-aspnetcore-runtimemaintainer hxc@qq.comRUN mkdir/dataCOPY ./publish//dataworkdir /dataCMD [ "Dotnet", "WebApplication1.dll"]
2. Publish Folder
Under Dockerfile's sibling directory, create a new publish folder to hold the current DLL file.
3. Build the image from the Dockerfile with build
[root@localhost tsweb]# Docker build--rm-f ts.dockerfile-t a/netcore:v1. Sending build context to Docker daemon2.56Kbstep1/6: frommicrosoft/dotnet:2.1-SDK---> Bde01d9ed6ebstep2/6:Maintainerhxc@qq.com---> Using cache---> 3af0c3f7c416step3/6:RUNMkdir/data---> Using cache---> 97137ffc5449step4/6:COPY./publish//data---> Using cache---> 77a94f1a0b8fstep5/6:Workdir/data---> Using cache---> 6778c2054a7bstep6/6:CMDdotnet WebApplication1.dll---> Running in e4a69b32e702---> 9ed3a9769610removing intermediate container e4a6 9b32e702successfully built 9ed3a9769610
4. Finally boot the image, bind to the default 5000 port with 8888
[Root@localhost tsweb]# Docker run-d-P 8888:5000 --name a-webcore-v1 a/netcore: v1f94c727b98d5654aa560308752c2af7cde550b6cc06c520bd438e4ccf1fa616d
5. Then you clearly see that the 8888 port has been opened, but can not access, awkward ...
[root@localhost tsweb]# netstat-tlnpactive Internet connections (only servers) Proto recv-q send-q Local Address Foreign Address State pid/program name TCP0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1834/DNSMASQ TCP0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1135/sshd TCP0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1136/CUPSD TCP0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1582/master TCP60 0::: 3306:::* LISTEN 2451/mysqld TCP60 0::: $:::* LISTEN 1135/sshd TCP60 0:: 1:631:::* LISTEN 1136/cupsd tcp6 0 0::: 8888:::* LISTEN 9531 /docker-proxy-c tcp60 0:: 1:25:::* LISTEN 1582/master [root@localhost tsweb]#
6. The first step in solving this problem is to see if the container really opens up 5000 ports, which can be viewed via Docker logs or Docker PS
[root@localhost tsweb]# docker logs b-webcore-v1hosting environment:productioncontent root path:/datanow Listening on:http://[::]:80
The original open is 80 port ha ~ ~ ~ that is simple, the original container to delete, re-create a container and then map a bit better.
8,888:80
Finally solved, OK, this article is here, hope to help you.