Several methods of setting URLs in asp.net core

GIS class 2021-11-25 17:26:32

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

image

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

image

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

image

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

image

The operation results are as follows

image

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

image

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());
});
});

image

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 :

image

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 :

image

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 :

image

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 :

image

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

image

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

Please bring the original link to reprint ,thank
Similar articles

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25

2021-11-25