How many times you probably wished that you can develop your .NET project in the root folder or host multiple websites on Win XP. If at least one time, then read on.
The problem with developing in the root is that VS does not really support that (although doable). Also, very often, I work on several projects or just simply would like to have a project in its own folder so that when I need I can easily bring it up for debugging/modifying without messing up all other projects.
I have come across multisite.dll (probably very well known) which allows to map different host names to different URLs. So I could setup www.project1.com to /project1 folder, www.project2.com to /project2 folder ... But this DLL does not work with .NET. The only thing it works well with is simple HTML, which basically makes it worthless.
But that made me wonder, what prevents it from working with .NET. After reading some documentation on ISAPI, I figured that the problem lies in how it handles the request.
When browser hits the IIS with http://www.project1.com/default.aspx, IIS converts the URL "/default.aspx" to physical path "C:\inetpub\wwwRoot\default.aspx". Only then that DLL gets its grip on the request and modifies physical path to the correct path "C:\inetpub\wwwRoot\project1\default.aspx". The first thing ASP.NET application does is, check for web.config and global.asax in the root folder and since the URL has not been changed the ASP.NET looks for those files in "\" folder and not in "\project1" folder, thus skipping to load config.asax or web.config making any serious project development with that version of Multisite.dll useless.
After I had realized that, I understood that the solution is to modify URL and not a physical path. After reading documentation more carefully, I discovered that
SF_NOTIFY_PREPROC_HEADERS in conjunction with
SetHeader will do exactly what I need.
- Browser hits the IIS with http://www.project1.com/default.aspx
- DLL modifies it to http://www.project1.com/project1/default.aspx.
Everything else is the same as if we would hit the URL http://www.project1.com/project1/default.aspx from the beginning. ASP.NET finds proper global.asax and web.config and works without even knowing that modification has been done.
Implementation of New Multisite DLL
- This step need to be done only once when you install my multisite.dll. Copy multisite.dll and config.ini in some folder then register it as ISAPI filter with IIS.
- Let's say you have .NET project. Let's say Project1. And you want it to be on www.mysite.com.
- Modify HOSTS file in C:\WINDOWS\system32\drivers\etc. Add a new line:
- Modify config.ini in the same folder where mulitsite.dll is. Add a new line:
mysite.com = /Project1
Note, not www. Becasue www. will be stripped out automatically so it works as in real world.
- Restart IIS server. After that every time you hit http://www.mysite.com/mypage.aspx the URL will be converted to http://www.mysite.com/project1/mypage.aspx and everything will work. When you are done with the project, you simply need to copy everything to root folder of the production website. No modifications needed.
- Safely assume that your project is working from the root so that all images, css files, js files might be referenced from the root. No need to append the project name and then when moved to production rename all of them.
- Where possible use root "/" or "~" symbol of .NET in constructions like
Response.Redirect("/mypage.aspx"). Do not use your project name. The construction
Response.Redirect("~/mypage.aspx") will be translated to
Response.Redirect("/project1/mypage.aspx"). Because of that multisite DLL will not modify the path. I.e., the path www.mysite.com/project1/mypage.aspx will not be touched and will be passed as is to IIS. (The key here is that first folder in URL matches mapping folder).
- In current implementation you need to restart IIS so that all changes in config.ini will be loaded.
- Debug is fully supported but because when you press F5 in Visual Studio, it opens http://localhost/.... You need to either map localhost to /Project1 or have a separate browser window point to http://www.mysite.com/.. and do all testing there. It's up to you which way to go but if you mapped localhost then do not forget to unmap it. Or you will not be able to access any other file on IIS outside of Project1 folder.
- It's crucial to have in config.ini the project folder to start with '/' symbol.
- Do not prefix names with www. in config.ini. www. is stripped out from the request to make it work like real web application. If you want both www. and non www. version of the working site, you need to add two lines to host file though. One for www. name and one without www. name!
I found that this small utility is very useful in my development work. I can keep all my projects running on my development machine and if needed easily be modified and uploaded on a production server. No need to run a "Replace ALL" to eliminate the project name.
Also it came very handy on my production server Ms.Piercing where I have multiple websites and instead of using the ability of IIS to run multiple instances of WWW, I am using my DLL to do that (it seems to me more lightweight).
If you have any questions about this DLL feel free to send me an email (remove nospam) or visit my website. I am planning to write next version which will not require to restart IIS every time you modify config.ini.