In this article, we will see what Port Forwarding is and how we can use it with ASP.NET Core Web projects.
Introduction
As a .NET Core developer, quite often you might come across a situation, where you have developed a POC as a Web application/Web API and now you want to share the project result with someone like your teammate or the college professor.
For doing this you have two options -
-
Host the application on IIS on your machine. This way it will be accessible only on the local network. But it also involves a tedious IIS hosting process.
-
Buy a hosting server and deploy the application there. But again it's a tedious process and you might have to spend some bucks for this.
Considering that your application is still in the development phase, the above two options do not seem to be feasible.
In such scenarios, the Dev Tunnels come to our rescue.
What are Dev Tunnels?
Dev Tunnels is a port forwarding feature provided by Microsoft which can be used as a part of ASP.NET Core Web Projects. Port Forwarding enables the connections between the systems which cannot directly access each other. Port Forwarding is also sometimes called Port Tunnelling or Web Tunnelling.
Some of the examples where Port Forwarding can be used are –
-
You have to test your Web API with some external device.
-
You have to share the result of your Web Application with someone who is not part of your access network and wants them to access it over the internet.
-
You have developed a webhook for some external services.
-
Using this URL in Power Apps to connect to the API.
-
Sharing the progress work result of your Web App/API with your stakeholders.
There are many such scenarios where port forwarding can be useful.
How to use Dev Tunnels in Visual Studio?
For using Dev Tunnels in your ASP.NET Core Project, you need to first do some configurations in Visual Studio. Please follow along –
Step 1 - Download and Install Visual Studio 17.4
Microsoft has added this feature as a public preview in Visual Studio 17.4+ versions. So make sure you download the Visual Studio 17.4+ version from the Microsoft Visual Studio Downloads page as per your machine's requirements.
For detailed instructions on how to download and install Visual Studio 2022, you can follow this article.
Step 2 - Enable the dev tunnels preview feature in Visual Studio
Once you have downloaded and installed the required version of Visual Studio, you can enable the Dev tunnels by navigating to Tools -> Options. This will open the options window. In the options window, on the left-hand side go to Environment -> Preview Features, and in the Preview features on the right side, enable the checkbox for Enable dev tunnels for Web Applications.
Step 3 - Configure Dev Tunnels in launchSettings.json
After the configuration is done, go your ASP.NET Core web application, and open the file launchSettings.json and update it as shown below –
As you can see above, we have added two properties in the profiles -
-
devTunnelEnabled – This is a Boolean value. When it is true, a tunnel will be used when the project is launched.
-
devTunnelAccess – This is applied for the port(s) being used by that specific project.
-
Private – It means only the signed-in user has access to that tunnel.
-
org – It means the user accessing that tunnel must have access to the organization in which the tunnel was created.
-
public – It means that anonymous access is allowed to the tunnel.
-
The default value for the devTunnelAccess is private if it is not provided.
Step 4 - Get URL to access the application Publically
Now run your application locally and a public URL will be allocated for your project by visual studio. It will open the below warning page initially as shown below on which you can click continue –
Once you click on continue, you should see the Home page for your application but with the public URL. Now you can share this public URL across as you need.
Thanks for reading this article. I hope you find it useful. Also, checkout my other articles for such interesting and practical content and do share it across.