I Installed .NET Core SDK v3.1
on an Ubuntu VPS,
and I am now experimenting with ASP.NET Core
projects.
I use
dotnet new webapp -o <ProjectName> --no-https
to create an ASP.NET Core project,
and
dotnet run
to run it, using .NET Core's built-in Web Server.
I have a small problem, which is the fact that .NET Core's built-in Web Server binds to IP 127.0.0.1
(localhost),
and not to the Public IP of my VPS.
This means that I cannot connect to my ASP.NET Core project from a browser on a remote computer, only from a browser on that VPS.
If instead of binding to 127.0.0.1
the Web Server would bind to 0.0.0.0
,
then it will be good, since 0.0.0.0
means "All Network Interfaces", and not just the localhost IP.
After searching in Google, people recommend to go to the project's folder,
then enter the Properties folder,
and there, to Edit a file called launchSettings.json
.
In that file, we need to change:
"applicationUrl": "http://localhost:5000"
to
"applicationUrl": "http://0.0.0.0:5000"
After doing this and re-running the project, the problem is solved,
and I can connect to the web server from a remote computer.
So this solution works,
yet I have 1 problem with it:
It requires editing every project's launchSettings.json
file.
I am looking for a solution that does not require changing every project for this,
but more like changing the Web Server's settings, once..
Does such a solution exist?
For example some Settings file for the Web Server, that defined which IP and Port to bind to,
or If not that, then maybe there's some Template for ASP.NET Core projects, and I can go to that Template's folder, and then edit there the launchSettings.json
file of the Template,
so all new ASP.NET Core projects that will be created from that moment on, will be created with the right IP (0.0.0.0).
Any solution that can be done Once, instead of Every Time, will be great.
The launchSettings.json
is only really meant to be used for development purposes. At that time, you are usually developing only locally and are not looking for external devices to access your server. That’s why the default on the loopback interface makes sense.
For production or general non-development purposes, you should properly bundle the application using dotnet publish
(or through Visual Studio). At that time, the published application will not include the launchSettings.json
, so that configuration doesn’t actually apply.
Instead, you should set the URL your application should listen on through configuration. For example using the environment variable ASPNETCORE_URLS
:
# E.g. on Linux:
ASPNETCORE_URLS=http://0.0.0.0:80 dotnet MyApp.dll
Or by passing the URL to the --url
command line argument
MyApp.exe --urls http://0.0.0.0:80
You can also put this into the appsettings.json
or an environment-specific appsettings.ENV.json
:
{
// …
"urls": "http://0.0.0.0:80"
}
Since the appsettings are usually committed to source control, this allows you to keep the configuration across machines.
As for changing multiple projects at once, the only option would be to set a global environment variable with the updated URL. But since multiple applications cannot be hosted on the same interface/port combination, this probably wouldn’t be the best idea since the value from the enviroment variable would overwrite the value from the appsettings, making this more complicated to reason about.