Access .NET Projects via LAN

As we already know .NET project runs on localhost. We cannot access it from any device connected to the same network.

Today, I am going to demonstrate how you can access your .NET project via LAN.

  1. Create a Web Application in Visual Studio.
  2. Now create a Simple Endpoint. I have created a controller called CalculatorController and created an endpoint that returns the sum of two numbers.
[Route("Calculator/Sum")]
[HttpGet]
public async Task<HttpResponseMessage> Sum(int num1,int num2)
{
     return Request.CreateResponse(HttpStatusCode.OK,(num1+num2));
}

3. Let's run the project. My project is running on https://localhost:44367/,let me hit the endpoint to get the result if it's working on localhost, so we can proceed further.

Local Host

4. Now, let's move toward the configurations of how can we access the website through LAN. Just open the BaseDirectory of your Project, then Go to .vs/{ProjectName}/Config, and now open applicationhost.config file.

Application Host

5. Just add a new binding which will bound to your IP address.

<binding protocol="http" bindingInformation="*:50745:{youripaddress}" />

6. Close the Visual Studio, Run the Visual Studio as an administrator, then build and run the project. Access your endpoint through your IP address it will work.

7. If you are not able to access the project through any other device in the network. You just have to go to the Windows Defender firewall and add an inbound rule for the port on which the project is running.

Window Defender firewell