Follow me on Twitter

First time here?

Check out the Archive, and Subscribe to the RSS feed.

Adding a Website to IIS7 programmatically

This post was written on April 16, 2008 19:32 by MartinHN

Some time ago, I blogged about Adding an Application Pool to IIS7 programmatically. The result was a new Application Pool, that uses the Integrated Pipeline in IIS7.

In this post I will show you how to add a new Website, that uses the Application Pool from the other blog post.

I've loaded the Console Application I used to add the Application Pool, and moved the logic from Main to a method called AddApplicationPool, to split it up nicely. It is actually even easier to add a website programmatically. Below is the code for doing just that:

private static void AddWebSite()
{
  ServerManager mgr = new ServerManager();

  if (!Directory.Exists(@"c:\inetpub\wwwroot\iis7test"))
  {
    Directory.CreateDirectory(@"c:\inetpub\wwwroot\iis7test");
  }

  // Add a new Site to the server, configured to use our the iis7test home directory.
  Site site = mgr.Sites.Add("MyWebSite", @"c:\inetpub\wwwroot\iis7test", 80);
  
  // Set the application pool name of the site, to use the MyAppPool application pool.
  site.ApplicationDefaults.ApplicationPoolName = "MyAppPool";

  // Clear all bindings.
  site.Bindings.Clear();

  // Make the site listen to incoming HTTP requests using host header iis7test, on port 80.
  site.Bindings.Add("*:80:iis7test", "http");
  
  // Set auto start to true.
  site.ServerAutoStart = true;

  // Commit the changes
  mgr.CommitChanges();
}

Notice how we add Bindings to the website. Bindings is the information that tells IIS7 when to serve our website. We use this string to configure bindings: *:80:iis7test. The first * tells IIS to listen on all IP addresses on your system. 80 is the port number, and iis7test is the host header value for this site.

To browse our website, we need to add iis7test to the computers hosts file (located in %WINDIR%\System32\Drivers\etc), and point it to 127.0.0.1.


kick it on DotNetKicks.com
Tags: , ,
Categories: C# | IIS7
Actions: E-mail | Permalink | Comments

Comments

November 12. 2009 22:59

fast payday loans

I guess there's always an easier way ...

fast payday loans

November 14. 2009 11:19

payday loans

I like your blog but how do I subscribe?

payday loans

November 17. 2009 04:55

replica watches

Oops~Your post is of great value for a guy like me which is learning some basic web knowledge to get my own web to work.Thanks for your great job~

replica watches

November 17. 2009 05:27

tiffany jewelry

Seems a little bit complicated for me, but it's useful~~

tiffany jewelry

November 17. 2009 09:40

Chinese Furniture

I see on your blog as it meet my interests in full.

Chinese Furniture

November 18. 2009 08:58

payday loans

thanks!  very helpful post!! like the template btw ;)

payday loans

November 18. 2009 10:44

tiffany jewelry

nice info. thanx for sharing with all of us. keep doing your great job.

tiffany jewelry

November 22. 2009 10:31

cash loans

Nice resource. rss feed added

cash loans

November 22. 2009 21:07

cash loans

Nice resource. rss feed added

cash loans

November 24. 2009 04:10

Acai Berry

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

Acai Berry

November 24. 2009 04:11

Acai Berry

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

Acai Berry

November 25. 2009 20:46

Search Engine Optimisation in the UK


This is the best post on this topic i have ever read.

Search Engine Optimisation in the UK

November 27. 2009 08:02

Acai Berry

Let me try it.

Acai Berry

November 28. 2009 11:03

Colon Cleanse

The code is working well.

Colon Cleanse

November 30. 2009 07:33

Colon Cleanse

Where should I paste this code to add my website programmatically?

Colon Cleanse

December 1. 2009 12:36

Acai Berry

I wanted to add my website to IIS7 pro-grammatically. Your blog has really solved out my problem.

Acai Berry

December 5. 2009 07:08

Acne

Thanks for given the code. Now I will try it in my website.

Acne

December 5. 2009 07:10

Acne

I was looking for this kind of information since a long time. You have finished my search by posting this link here. Any ways great post. Thanks for posting this here.

Acne

December 14. 2009 12:18

acai

I see on your blog as it meet my interests in full.
[url=www.goarticles.com/cgi-bin/showa.cgi?C=1829378]Acne[/url]      

acai

January 5. 2010 12:54

executive vacancies

An executive recruitment job site for UK and global positions. Executive search and head-hunters.

executive vacancies

January 5. 2010 19:05

payday loans

Thank you for your help!

payday loans

January 10. 2010 13:08

payday loans

Do you make money out of this blog? just curious

payday loans

January 21. 2010 14:04

SEO

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work…

SEO

January 27. 2010 09:27

corner workstations

Thanks for the post. Keep the great work.

corner workstations

January 27. 2010 15:33

job websites Dubai

Thank you for telling us how to add website to IIS 7. Might able to done my job on time.

job websites Dubai

February 3. 2010 08:15

Extenze

Your code proved to be of great help and saved a lot of mine.

Extenze

February 7. 2010 07:15

Alcohol Abuse Iowa

Interesting post and I really like your take on the issue.  I now have a clear idea on what this matter is all about. Thank you so much.

Alcohol Abuse Iowa

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.1.24

About the author

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2009