Configure the ASP.NET Core Web URL
By default, when we run any asp.net core project from the command line it'll be running on http://localhost:5000
.
If we want to run from the other port or from the other machine, we need to configure it properly.
Here is how.
The main battle is in the Main()
method in the Program.cs
which its source code from the template are shown below.
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
We have 2 methods to configure for the launch url.
- Configure in the source code.
- Configure in the configuration file.
Method A: Configure in the source code.
The easiest method is to use UseUrl()
method like this
.UseUrls("http://localhost:5001", "http://*:5002")
Which should yield the updated source code look like this
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseUrls("http://localhost:5001", "http://*:5002")
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
But this method is NOT production-ready. So let's take a look at the second method.
Method B: Configure in the configuration file.
We could use a setting in the file names hosting.json
with the source code for reading the configuration like this one.
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
In some environment setup we may use
AppDomain.CurrentDomain.BaseDirectory
instead ofDirectory.GetCurrentDirectory()
.
And use the above config
in our WebHostBuilder
with .UseConfiguration(config)
to yield the complete source code like this.
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: true)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
The final piece is hosting.json
which might look like this.
{
"server.urls": "http://localhost:5001;http://*:5002"
}
Running form Visual Studio
When you configure all those steps and run from Visual Studio, you might surprise that it will seam to run from a random port. Because Visual Studio has 2 running mode,
- Run from IIS / IIS Express
- Run from Dotnet Core.
Both of them can be configure from the same launchSettings.json
file. The file should looks like
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:19853/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebWithServerUrl": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Just specify the applicationUrl
or launchUrl
, separate the url by semicolon (;) like the following settings and it should work fine.
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:19853/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"WebWithServerUrl": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5001;http://*:5002",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}