Sunday, July 12, 2015

Enable remote access to IIS Express for debugging in Visual Studio 2013

In Visual Studio 2013 you can run or debug a project just clicking to the play button. But by default the project is reachable just from localhost and not reachable from any computer, phone or other device in local network. This is annoying when you want to test and debug using a mobile device. Here I show how to configure the environment to enable remote access to a local website running from VS2013.



First of all

Find your ip in the network, you can do that typing  ipconfig  from the command prompt and looking from ip in the network adapter you are using (ethernet card or wifi or something else).
In this example we will assume that IP is 192.168.1.100
Look at the port of your website in IIS Express, in this example we will assume that it is 50836.
Change this values with your values.

Open Windows Firewall

You have to add the port 50836 to your firewall rules to accept incoming and out coming traffic.

Add Url Rule

From the command prompt (run command prompt as Administrator) type the following to add an access control list to url:

netsh http add urlacl url=http://192.168.1.100:50836/ user=everyone

later you can delete this rule typing:

netsh http delete urlacl url=http://192.168.1.100:50836/

alternatively you can open Visual Studio as Administrator.
Altough all forums and documentations say to add this rule, in my case it didn't work, with all possibile combinations. So I ended up opening Visual Studio as Administrator.

NOTE: If after all the procedure you still have a "Service Unavailable" or "Bad Request - Invalid Hostname" then you have still problems with urlacl, then to avoid it you can run Visual Studio as Administrator to bypass it.

Modify applicationhost.config

You have to edit a file, applicationhost.config that is usually located into the directory:
%HOMEPATH%\Documents\IISExpress\config

In this file look for your project you want to expose, i.e. WebApplication1, you will find something like this:

<site name="WebApplication1" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Projects\WebApplication1" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation=":50836:localhost" />
    </bindings>
</site>

Add another binding with your ip address to the bindings tag:

<site name="WebApplication1" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Projects\WebApplication1" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation=":50836:localhost" />
        <binding protocol="http" bindingInformation=":50836:192.168.1.100" />
    </bindings>
</site>

You could also substitute ip address with an asterisk "*", like this, in order to allow all aliases:

<binding protocol="http" bindingInformation=":50836:*" />

Port Forwarding (from external network)

If you want to reach the service from outside your local network too, add a port forwarding rule to router, in order to tell the router to redirect all the traffic for the port 50386 to the computer with IP 192.168.1.100.
This step involves entering in the control panel of your router and search for something like "Port Forwarding", in order to insert the Port and IP combination.
This step changes for each router vendor, but basically they do the same thing.
You often can access to your router control panel typing the address: 192.168.1.1 or 192.168.0.1 or 10.0.0.1 but it really depends from your network configuration.

At this point all should work!  :)



2 comments:

  1. Another, simpler option is to use our Visual Studio extension 'Conveyor', which does everything for you without touching any configuration files or your project. https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

    ReplyDelete
  2. OK, very useful details, however, even after following thses instructions, VS should be run as administrator otherwise I get a message "cannot connect to IIS express. I close VS and fire it in admin mode it works fine, hoe this can be overcomed? Thansk again

    ReplyDelete

(c) Copyright 2020 - MyTroubleshooting.com