Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

New Multisite DLL Or How to develop .NET project in a Root

0.00/5 (No votes)
8 Apr 2005 1  
Utility to ease .NET development.

Introduction

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.

Solution

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 GetHeader, SetHeader will do exactly what I need.

  1. Browser hits the IIS with http://www.project1.com/default.aspx
  2. 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

  1. 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.
  2. Let's say you have .NET project. Let's say Project1. And you want it to be on www.mysite.com.
  3. Modify HOSTS file in C:\WINDOWS\system32\drivers\etc. Add a new line:
       127.0.0.1    www.mysite.com
  4. 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.

  5. 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.

Important Notes

  1. 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.
  2. Where possible use root "/" or "~" symbol of .NET in constructions like Response.Redirect("~/mypage.aspx") Or 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).
  3. In current implementation you need to restart IIS so that all changes in config.ini will be loaded.
  4. 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.
  5. It's crucial to have in config.ini the project folder to start with '/' symbol.
  6. 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!

Conclusion

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here