Preface

In the use of ASP.NET Core 3.1 When developing , You need to configure the port and protocol that the server listens to , Official help files Give a brief description , The document mentions 4 Species designation URL Methods

  1. Set up ASPNETCORE_URLS environment variable ;
  2. Use dotnet --urls Command line arguments ;
  3. Use urls Configure as a key ;
  4. Use UseUrls Extension method ;

For clarity URLs Setup method , Create a AspNetCoreUrl Of ASP.NET Core Web API The procedure is described , By default , start-up ASP.NET Core after , Listen to the following URLs:

http://localhost:5000/

https://localhost:5001/

URL Format

There are three main types URL Format expression , You can view the official documentation (https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#url-prefixes)

Format 1:{scheme}://{loopbackAddress}:{port}, for example http://localhost:5000/https://localhost:5001/

Format 2:{scheme}://{IPAddress}:{port}, for example http://127.0.0.1:5000/https://192.168.1.100:5001/

Format 3:{scheme}://*:{port}, for example http://*:5000/https://domain.com:5001/

Prerequisite

At the beginning of the test URLs Before setting the method , You need to be successful AspNetCoreUrl Program , Then open the command-line tool in the generated root directory for corresponding testing

Pictured , The root directory of my program here is D:\AspNetCoreUrl\AspNetCoreUrl\bin\Debug\netcoreapp3.1, The terminal used is Microsoft's official Windows PowerShell

Method 1 Using environment variables

Don't modify AspNetCoreUrl Any source code case ( That is, the default state of the program when creating the project ) Generating programs , Navigate to the generated root directory , Open command line terminal

# Environment variables only take effect in the current command line window 
$Env:ASPNETCORE_URLS = "http://localhost:7000;https://localhost:7010"
# Or use DOTNET_URLS Environment variables also take effect
$Env:DOTNET_URLS = "http://localhost:8000;https://localhost:8010"
# function AspNetCoreUrl Program
dotnet AspNetCoreUrl.dll

If you use Windows Command line ( namely cmd Command line ), Use the following method to set

# Environment variables only take effect in the current command line window 
set ASPNETCORE_URLS=http://localhost:7000;https://localhost:7010
# take ASPNETCORE_URLS Save variables to user environment variables
setx ASPNETCORE_URLS "http://localhost:7000;https://localhost:7010"
# Add /m Parameters , take ASPNETCORE_URLS Save variables to system environment variables
setx ASPNETCORE_URLS "http://localhost:7000;https://localhost:7010" /m
# function AspNetCoreUrl Program
dotnet AspNetCoreUrl.dll

Be careful : Use setx After setting environment variables , Need to open a new Windows The command line window takes effect using the environment variable

stay Linux Use the following command to set the environment variable in the system

# Environment variables only take effect on the current terminal , It needs to be reset after closing the terminal 
export ASPNETCORE_URLS="http://localhost:7000;https://localhost:7010"

Method 2 Use command line arguments

In the root directory of the generator , Use dotnet --urls command , And bring urls Parameters

dotnet AspNetCoreUrl.dll --urls "http://localhost:7001;https://localhost:7011"

Method 3 Use profile

In the root directory of the generator , open appsettings.json file , add to url Configuration item , Then double click. AspNetCoreUrl.exe function

{
"urls":"http://localhost:7002;http://localhost:7012"
}

The operation results are as follows

Method 4 Use UseUrls

This method requires modifying the source code , open Program.cs file , modify CreateHostBuilder Method content , Mainly to add UseUrls Extend the method and then generate the program .

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
// Use UseUrls Set the listening port and protocol
webBuilder.UseUrls("http://localhost:7003", "https://localhost:7013");
});

Be careful : Before operation, you need to appsettings.json Restore the file to the default state , That is, there is no configuration urls The state of , Otherwise, the settings in the configuration file will override the methods in the code .

Method 5 Use Kestrel

This method is not in official documents , Because this method is limited to Kestrel When it comes into effect , Creating ASP.NET Core Application time , By default Kestrel To host applications , There are also corresponding application scenarios using this method , Here is a brief introduction to the use of this method

open Program.cs file , modify CreateHostBuilder Method content

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
// To configure Kestrel service
webBuilder.UseKestrel(kestrelServerOptions =>
{
kestrelServerOptions.ListenLocalhost(7004);
kestrelServerOptions.ListenLocalhost(7014, listenOptions => listenOptions.UseHttps());
});
});

priority

Previously introduced 5 Both methods are running independently , If the 5 Two methods are used at the same time , What kind of effect will it have , Let's test it , For ease of testing , Each method uses a different port to distinguish , The running results using all methods are shown below :

You can see , What works is the use of Kestrel Method , You will now Kestrel This comment is omitted , The operation results are as follows :

You can see that what works at this time is to use the command line parameter method , If you don't use command line arguments, methods , The operation results are as follows :

What takes effect is the settings in the configuration file , Now delete... From the configuration file urls Configuration of , The operation results are as follows :

What works is UseUrls Extension method , Now comment out UseUrls method code , The operation results are as follows :

What works is the environment variable , If you do not set the environment variable , The default value is used , namely http://localhost:5000/ and https://localhost:5001/.

summary

This paper introduces ASP.NET Core Several common settings URLs Methods , You can choose one or concentrate according to the actual situation of the project , If several are used at the same time URLs Setup method , You need to pay attention to the priority of configuration , It was tested that Kestrel > Command line > The configuration file > UseUrls > environment variable > The default value is .

If you want to try it yourself , Code address :

gitee:https://gitee.com/gyhgis/AspNetCoreUrl

github:https://github.com/gyhgis/AspNetCoreUrl

ASP.NET Core Set up URLs More related articles on several methods of

  1. ASP.NET Core Set up and initialize the database - ASP.NET Core Basic course - Simple tutorial , Simple programming

    original text :ASP.NET Core Set up and initialize the database - ASP.NET Core Basic course - Simple tutorial , Simple programming ASP.NET Core Set up and initialize the database We set up and configured it in the previous chapter EF ...

  2. ASP.Net Core Next Authorization In several ways - Simple books

    original text :ASP.Net Core Next Authorization In several ways - Simple books ASP.Net Core Next Authorization In several ways Authorization The goal is to verify Http Can the request ...

  3. The improvement of performance tuning ASP.NET Web Application performance 24 Two methods and techniques

    The improvement of performance tuning ASP.NET Web Application performance 24 Two methods and techniques   poptest It is the only training institution in China to train test and development engineers , Students are competent for automated testing , Performance testing , Test tool development and other work as the goal . If the ...

  4. IIS7.5 Use web.config Two ways to set pseudo static

    from Make money on the Internet .http://www.whosmall.com/post/121 In recent days, several projects developed by the company are running in IIS7.5 On , Because the whole station is pseudo static , So find two ways to do it from the Internet . These two ...

  5. asp.net Comparison of three ways to jump to a page

    at present , For learning asp.net For many of my friends , The way to jump to a page is not well understood . This article will introduce the use of asp.net Three ways to jump to a page , And compare the forms between them , I hope I can help my friends . ASP.NET ...

  6. Android Medium full screen Cancel the title bar ,TabHost Set in NoTitleBar Three ways ( turn )

    Android Medium full screen Cancel the title bar ,TabHost Set in NoTitleBar Three ways http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...

  7. Introduction to server programming (13) Linux There are three ways to set a socket timeout

    Abstract :     This article describes the socket in the I/O There are three ways to set timeout on operation . The picture may be a little wide , For Invisible Children's shoes, you can click on the picture to view the complete picture .. 1 call alarm Use SIGALRM by connect Set timeout Setup method : ...

  8. loadrunner: Several ways to set checkpoints

    Judge whether the operation is successful , Checkpoints are essential .loadrunner Text checkpoints and picture checkpoints can be set in , Let's record loadrunner Several ways to set checkpoints . Preface : When playing back the script , It must be confirmed that run-time set ...

  9. How to ASP.NET Core Set up the client IP White list verification

    Link to the original text :Client IP safelist for ASP.NET Core author :Damien Bowden and Tom Dykstra translator :Lamond Lu This blog post shows how to AS ...

  10. ASP.NET Core Set default start page ( Such as default.html)

    The test page foo.html stay Startup.cs Internal use middleware The code is as follows : DefaultFilesOptions defaultFilesOptions = new DefaultFil ...

Random recommendation

  1. oracle No client installation plsql Connect

    In the process of development , There is often no need to install... On your own computer oracle, It's too big . Here is an introduction to the use of plsql Connect oracle Methods , Be similar to navicat Connection mysql. To download plsql, Installation , Provided here ...

  2. MySQL Learning notes —— Installation and configuration environment

    1. The installed version is mysql-5.6.24-win32.1432006610 Compressed version You can view the tutorial http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345b ...

  3. Tree array +STL FZU 2029 Ticket issue

    Subject portal The question : Chinese questions analysis : Do it after an exam week , There was no idea at first , I feel like I can use a line segment tree / Tree array maintenance , The tree array maintains the minimum value and does not write the line segment tree , Result timeout . Later, it was found that only a few people maintain the prefix and use the priority queue /set Maintenance most ...

  4. The finger of the sword Offer: Interview questions 11—— Integer power of value (java Realization )

    Title Description : Implementation function double Power(double base, int exponent), seek base Of exponent Power , Do not use library functions , At the same time, there is no need to consider the problem of large numbers Ideas : The key content of this question is ...

  5. sqldependency Supported by select

    https://msdn.microsoft.com/library/ms181122.aspx   Supported by SELECT sentence Meeting the following requirements SELECT Statement supports query notification : Must be explicitly stated SEL ...

  6. phonegap Installation and use eclipse

    1. download nodejs... Then log out and let nodejs You can use 2. download jdk,ant. And Android sdk. jdk Is for ant Compile time requires , Set up JAVA_HOME environment variable C:\Program Files\Java\ ...

  7. Ten words teach you to learn Linux Stream redirection

    1. When I see the redirection, I immediately think of web Inside redirect, you 're right , however Linux The purpose of data stream redirection is not to jump to another web page , It's used to store important screen information . Output unnecessary screen information to a file or “ Black holes ” in . Put the error message ...

  8. Django In the extended Paginator Implement paging

    Reference:https://my.oschina.net/kelvinfang/blog/134342 Django Many functions have been implemented in , Basically, as long as we need the function , Can find the corresponding package . To be in Dj ...

  9. BZOJ3498PA2009 Cakes—— Three membered rings

    Title Description N A little bit m side , Each point has a point weight a. For any ternary ring (j,j,k)(i<j<k), Its contribution is max(ai,aj,ak)  Find the contribution sum of all ternary rings .N<100000,,m< ...

  10. Javascript Advanced programming learning notes (76)—— Forms (4) Select text

    Text box script stay HTML There are two ways to implement Chinese text box : <input> <textarea> The two implementations are consistent in most cases , But there are still many important differences between the two about <inp ...