Access ASP.NET Core website hosted on IIS Express from a phone

While developing the new ASP.NET Core based app for elmah.io, I wanted to make sure that everything is mobile friendly on a phone too. While emulators and the Device toolbar in Chrome get you part of the way, testing on a real device is inevitable. In this post, I'll show you how to access an ASP.NET Core website running on your local machine from a phone.

IIS Express doesn't allow remote connections to locally hosted websites as default. Luckily, Ionut-Cristian Florescu developed a small tool called iisexpress-proxy. As the name already spells out, iisexpress-proxy is a simple local proxy for IIS Express. The app is based on node.js and can be downloaded from GitHub.

To launch iisexpress-proxy, download and install node.js if not already on your machine. If you're using a recent version of npm (^5.2.0) then you don't need to install iisexpress-proxy. You could simply use this command:

npx iisexpress-proxy localPort to proxyPort

With an older version of npm, you'll need to install iisexpress-proxy as a global module using the following command:

npm install -g iisexpress-proxy

After installation, run the command below:

iisexpress-proxy localPort to proxyPort

To understand localPort and proxyPort let's create an example. Let's say that your application's IIS Express port is 44302 and you want to test it using port 80 then you should run this command:

iisexpress-proxy 44302 to 80

The program will list the external addresses you can use for testing your application on remote devices. By default iisexpress-proxy protocol is set to HTTP, so if you're running HTTPS, then you need to include the full URL:

iisexpress-proxy https://localhost:44302 to 80

The problem here is that on localhost you're not using SSL so make sure that you stick to HTTP protocol. Also, avoid redirecting from HTTP to HTTPS when running locally. In ASP.NET Core you can achieve this by calling app.UseHttpsRedirection() in Startup.cs for non-development environments only:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        // ...
    }
    else
    {
        // ...
        app.UseHttpsRedirection();
    }
    
    // ...
}    

To access the local IIS Express from another host, you will need the IP of your machine. The easiest way to get is to to open a new command prompt and run the following script:

ipconfig | findstr /R /C:"IPv4 Address"

That's it. Your website is now accessible from all of your devices: