Monday, January 10, 2011

ASP.NET Website. What, Why and How.

Everybody needs to start with this one. Atleast who wish to learn ASP.NET. So, how to create a website. Well, there are many ways. One simple way is to trust Visual Studio 2005 or 2008 or 2010.

1. Open Visual Studio -> File -> New Website.
2. In the New website window, if you wish to host the website on IIS, choose Http for Location and http://localhost/Website
3. Choose your language and hit OK.
Its done.

What it does for you is, you will see a couple of files VS adds to your website by default.
1. Default.aspx
2. Web.config

Default.aspx is a webpage like any other html page you visit and its the .NET version for the same

The website is hosted on IIS. Great! But where on the file system? By default, VS creates all websites (when location is set to Http) at C:\inetpub\wwwroot. What does this mean?

Apart from this website, you can create multiple websites on IIS (the server) and all will go to the same C:\inetpub\wwwroot but exist as different Virtual Directories. Note this down. You will hear a lot about this during your dev days. Coming back, you can still call your new website as a Website. Its just that it exists as a separate virtual directory under IIS.

One thing here. IIS listens on port 80. And there could be many ports on a machine. For example, a SQL Server listens on 1433. And so on.

So, when you try to access your website http://localhost/website, you are sending a request to IIS which is the localhost listening on port 80. Similarly all the websites that you create on IIS can be accessed via the same http://localhost/anyotherwebsite.

Ok. Enough of this. Any other way of creating a website? Yes, the other way around. Here it is.

1. Create a folder anywhere in your system and name is NewWebSite.
2. Open IIS. (Tip: Press Windows + R. Type inetmgr and hit enter)
3. Expand left tree node till you see Default Web site.
4. Right click and select New Virtual Directory
5. Give a name to the website (preferably the same name as the folder 'NewWebSite')
6. Browse to the newly created folder NewWebsite for PhysicalPath and hit OK.

Your new website is created in IIS. Now, open Visual Studio -> File -> Open Website -> Choose IIS -> Select NewWebsite from the list.

This opens a website in Visual Studio but with no files under it in the solution explorer. This is unlike the first case where you have seen Default.aspx and Web.config already added to the website. RIght click the website in Solution Explorer and add the items that you wish. You can even add Madonna.aspx to your new website. And Madonna can accessed at http://localhost/NewWebSite/Madonna.aspx

The great thing about this approach is you just knew that, a website is nothing but a folder of files. Files could be aspx, doc, htmls, images, css, js, mp3, mpg, flv, config and whole lot of other files. The thing that you should understand is, a website is nothing but a folder with files and like any other folder that you copy and carry in a USB stick, a website too can be moved from a computer to a USB stick, from one computer to another, from a developer machine to production server and virtually to anywhere. You just need to know how to attach a website to IIS which is just described above.

Let me know if you have any questions.

No comments:

Post a Comment