Starting from Visual Studio 2012 the good ol' Cassini Web Server has been replaced by IIS Express. While Microsoft spent quite an amount of words describing the benefits brought by this not-so-minor change, the strictness of the security policies featured by the newcomer raised more than an issue between those accustomed to the old environment: the new Property window doesn't allow devs to change the local hostname/folder or to assign a custom TCP port anymore, which can be crippling in most scenarios. Another serious issue is due to the fact that IIS Express, unlike Cassini, doesn't natively accept external request, meaning that no network-connected client can be used to debug the website anymore.
HTTP Error 503. The service is unavailable.
We're talking about a serious issues here, expecially in times like this where everything is about responsive, mobile-friendly, postback-free web-based stuff which almost always need to be tested on many different devices.
The three reasons preventing IIS Express to accept external requests are:
- Its configuration XML file, where Visual Studio saves the (auto-generated) web application settings once we run it in Debug or Release mode the very first time.
- The security restrictions (ACL settings) on the HTTP.SYS component, who doesn't allow a non-elevated application - like Visual Studio executed like the current user - to handle network traffic.
- The default Windows Firewall (or any other firewall installed) settings, hardly allowing an arbitrary set TCP port to accept outcoming calls.
Let's see what we can do to go through all these problems:
- Be sure you don't ave any open Visual Studio instance.
- Open the file %userprofile%\Documenti\IISExpress\config\applicationhost.config , being %userprofile% your user folder - like C:\Users\YourUsername\.
- Look for the entry corresponding to the web application we want to modify and change its binding element in the following way:
<binding protocol="http" bindingInformation="*:<port>:*" /> All we need to do there is to replace a couple of localhost with *, leaving the automatically assigned <port> as it is. If you really need to change that port, replace it with another valid, free TCP port (like 8080) and perform the following step. Otherwise, skip it and go to the step next to it.
- If you changed the automatically assigned TCP port you also need to open your web application project (.csproj) and solution (.sln) files and ensure there are no references to the previous port: they can be there or not depending on your choosen project type (Web Application, MVC App, Web Site, etc.). If that's the case, replace the old port with the new one.
- Open a Command Prompt (with Administrator permissions) and type the following:
netsh http add urlacl url=http://*:<port>/ user=everyone Putting the application TCP port in place of <port>. If you get an error messae (1789) it means that the Everyone user group is not present on your system, something who can occur in some Windows localizations. If that's the case, just replace everyone with the corresponding user group name. Instead than doing this step you could also try and execute Visual Studio with administrator permissions.
- Open Windows Firewall advanced configuration panel and add an inbound rule to enable the inbound traffic for the application IISExpress.exe OR for the TCP port used by your web application. If you disabled it for another product, do the same with it. If you ain't using any - are you sure? - you can either take the chance to fill the void and then perform the above or just skip this step.
Once you did this you can launch Visual Studio and run your application in Debug or Release mode: you should be able to access it from any external, network-connected device using the following web address:
Visual Studio 2015 Update
As pointed out by Zachary Pittman (many thanks to him for reporting this), if you're using the IIS Express bundled with Visual Studio 2015, you will find the relevant applicationhost.config file within a /.vs/ subdirectory within your project root folder. However, whenever you've unsure about the path, you can always right-click to the IIS Express icon in the system tray: from there, > Show all applications, select your site and look for the config file location.
Let's just say it: IIS Express, with all its flaws and missing configuration features, is far from being the best web development server available. Anyone looking for a more robust, versatile solution will probably like to switch to something like IIS 7.5 (or above). The main issue there is due to the fact that in order to use IIS togheter with Visual Studio you could be forced to launch it with administrator permissions to avoid the following error message:
This happens because Visual Studio needs to have access to the IIS metabase, a priviledge reserved to the Network Administrators group.
UPDATE: If you don't feel like executing Visual Studio with administrative priviledges you can use this neat workaround to permanently grant the IIS Metabase folder permissions to the current user in a quick and easy way.
What else can we say? Happy (so to speak) coding!
EDIT: If you want to dive deeper into IIS Express mechanics and understand the reasons that moved Microsoft to make it become the successor of Cassini I would suggest you to read this excellent (despite its age) post by Scott Hanselman. Have a good reading!