Issue
I am trying to host an API (ASP.NET) on my Raspberri Pi 3B +. I would like to access this API swagger page from my laptop/phone/pc etc.
What I’ve tried until now:
Installed, compiled, built and ran the web application with dotnet on my RPi.
running dotnet --info
on the RPi provides us the following information:
.NET SDK (reflecting any global.json):
Version: 5.0.405
Commit: 63325e1c7d
Runtime Environment:
OS Name: raspbian
OS Version: 11
OS Platform: Linux
RID: linux-arm
Base Path: /usr/share/dotnet/sdk/5.0.405/
Host (useful for support):
Version: 5.0.14
Commit: d5b56c6327
.NET SDKs installed:
5.0.405 [/usr/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.14 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.14 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
When I run the program (filepath ~/first-rpi-api/bin/Release/net5.0/publish/first-rpi-api
)
I get the following:
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /home/pi/first-rpi-api/bin/Release/net5.0/publish
The default URL for the swagger UI would be : https://localhost:44335/swagger/index.html
However when I try to access the API from my PC on URL : https://RPi_IP:5000/swagger/index.html
I get ERR_CONNECTION_REFUSED
How do I fix this ? Have I missed something and how would I approach this problem ?
Solution
Using the previous answer (refering to omajid’s answer) provides us only a temporary solution.
Whenever you reboot the Raspberry Pi you would need to execute the command
export ASPNETCORE_URLS="http://*:5000;https://*:5001"
again and again.
To prevent this, I suggest using the IHostBuilder.UseUrls("URLS")
method.
In your case you would need to edit your Program.cs
and add the following line in the CreateHostBuilder method.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
//THIS LINE
webBuilder.UseUrls("http://*:5000;https://*:5001");
});
This will automatically ensures that you can access the API from whichever device you’d like.
You can read more about this in this article :
Answered By – schweppes0x
Answer Checked By – Cary Denson (BugsFixing Admin)