iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤖

Accessing ASP.NET Core on IIS Express from Other Devices on the Same Network

に公開

Background and Purpose

When developing with ASP.NET Core, there are times when you want to test by accessing your application from another device on the same network. Especially during the verification phase, preparing a dedicated server and deploying every time is tedious, so I will summarize the setup procedure for doing this in a local environment.

The Challenge

Normally, ASP.NET Core's IIS Express is only accessible via localhost, so you cannot access it from devices like smartphones over a private Wi-Fi network.

Prerequisites

Run Visual Studio as Administrator

Make sure to run Visual Studio with administrator privileges.

Retrieve your IP Address

Check and record your IP address. You can get it by running the following command in the Command Prompt:

ipconfig

Configuration Procedure

Edit Visual Studio Configuration File

First, ASP.NET Core projects contain a hidden folder named .vs, which includes the applicationhost.config file that needs to be edited. Add a bindingInformation entry with the IP address you retrieved, as shown below.

Before modification

<bindings>
  <binding protocol="http" bindingInformation="*:63508:localhost" />
  <binding protocol="https" bindingInformation="*:44367:localhost" />
</bindings>

After modification

<bindings>
  <binding protocol="http" bindingInformation="*:63508:localhost" />
  <binding protocol="http" bindingInformation="*:63508:192.168.0.131" />
  <binding protocol="https" bindingInformation="*:44367:localhost" />
  <binding protocol="https" bindingInformation="*:44367:192.168.0.131" />
</bindings>

Edit the Launch Configuration File

Edit the \Properties\launchSettings.json file located inside the ASP.NET Core project folder.

Before modification


    "iisExpress": {
      "applicationUrl": "http://localhost:63508",
      "sslPort": 44367
    }

After modification

    "iisExpress": {
      "applicationUrl": "http://192.168.0.131:63508",
      "sslPort": 44367
    }

Reserve the URL Prefix

Reserve the URL prefix so that external devices can access it. Use the Command Prompt with administrator privileges to reserve the URL prefix.

Command

netsh http add urlacl url=http://192.168.0.131:63508/ user=Everyone
netsh http add urlacl url=https://192.168.0.131:44367/ user=Everyone

Add Firewall Rules

Add firewall rules using the Command Prompt with administrator privileges.

Command

netsh advfirewall firewall add rule name= "ASP.NET Core WASM App" dir=in action=allow protocol=TCP localport=63508
netsh advfirewall firewall add rule name= "ASP.NET Core WASM App2" dir=in action=allow protocol=TCP localport=44367

After Finishing Your Debugging

Once you have finished debugging, remove the URL prefix reservation using the Command Prompt with administrator privileges.

Remove URL Prefix

netsh http delete urlacl url=http://192.168.0.131:63508/
netsh http delete urlacl url=https://192.168.0.131:44367/

Delete Firewall Rules

Delete the firewall rules using the Command Prompt with administrator privileges.

netsh advfirewall firewall delete rule name= "ASP.NET Core WASM App"
netsh advfirewall firewall delete rule name= "ASP.NET Core WASM App2"

Conclusion

I set up this environment to test API calls for a smartphone app using a PC. While regular APIs run fine on localhost, this setup is useful when testing with external devices.

Discussion