Update for 2014: I wrote this back when Visual Studio 2010 was all the rage. It came bundled with a little built-in web server that I believe is different from what more modern versions of Visual Studio use. I haven’t had a chance to use them yet, but I believe they are based on IIS Express. I’ve heard that this server doesn’t have the localhost restrictions that the original webserver did. With a little reading and some tweaks, you may be able to access it from your device over Wi-Fi. If you already have good information on this, please leave a comment with details! Thanks, and good luck!
Have you ever wanted to test and debug a mobile ASP.NET site on an iPhone, Android phone, Blackberry, or other mobile device? If so, you might have noticed that the development server packaged up by default with Visual Studio is a little bit… limited. It doesn’t allow anyone to access your local development web server, and will actually reject requests from outside your local machine outright.
Maybe this is a good idea for security. Maybe it was a decision made to keep people from using the development servers instead of getting a real IIS server. Maybe it was just easier to program when developing Visual Studio? I am really not sure what the official reason behind it was.
First, let me ask you a few questions about mobile web development issues with ASP.NET that used to be painful for me:
- Ever had to push a site up to a development server every time you make a change?
- Tired of waiting FOREVER for sites to publish after changes just so you can test them on your device?
- Frustrated over not being able to use breakpoints after publishing to a remote server?
- Sick of resynchronizing data every time you have to push a change to your development server?
- Ever wished you could share changes from your local machine without overwriting or waiting to republish on development?
I sure hope these are things that make you mad, because they were pissing me off to no end. After doing a few full mobile sites with ASP.NET I could literally take no more. I decided to come up with a solution. Maybe it’s not the best solution, but I needed something that would solve these problems for myself and people on my teams.
I decided to go for a program that would open up the internal local port to another port of my choosing globally. All traffic is passed directly from one port to the other, internally, while being completely loss-less and unchanged. Since it’s written with the .NET framework and I generally call it like it is… I dubbed this project SharpProxy.
Using SharpProxy is easy:
- Take a peek at what port your local ASP.NET server has decided to use.
- Enter it in as the Internal Port
- Change the External Port if desired
- Hit Start
That’s it! Once you have SharpProxy up and running, you can connect your device to your internal Wi-Fi network and access the server via IP and port. In my case, the process looks like this:
- Configure and start SharpProxy
- Connect my iPhone to Wi-Fi
- Point Safari to http://192.168.1.103:5000
If I wanted to, I could setup a port forward from my router on port 5000 and use my public IP address or a dynamic DNS service. Hopefully how this all fits together is making sense – I trust you (the developer) are at least partially familiar with network scopes, firewalls and all of that.
Potential Pitfall: I really don’t think this will be a problem for most people. Still, you should be aware of it. This isn’t a problem with SharpProxy, rather it is a problem with redirections in general.
When using this with our CAS authentication server at work, I noticed that sometimes the redirects from our projects were wrong. I corrected for our case by making sure I redirect back from CAS to whatever the requested host was – NOT localhost.
In code, you can detect what the base host is by doing something like this from an ASP.NET MVC site’s controller. I think it should also work from an older ASP.NET web forms project also.
string baseURL = "http://" + this.Request.Headers["Host"];
To conclude, I hope that SharpProxy can be useful for you. I’ve published the source on GitHub, so feel free to get a copy of it, or just download and use the executable (requires the .NET 2.0 framework or greater). It may work in Mono also, but I haven’t tested it.
It’s free as in beer! (That means it’s really free, no license.) If you like using it feel free to talk about, write about it, link to it, or donate a few dollars into my PayPal. I have no shame about asking for that – everybody’s gotta eat!
(Make sure you have the .NET 2.0 framework or greater)