tags:

views:

588

answers:

4

I have an ASP.NET website application, and there is a home page for my web site. I need to be able to change the default document of my website programmatically (C#) so that I can make another web page take priority above the one that already exists. I would then like to revert back to the previous default document order.

Example :

I have two home pages - Home1.aspx and Home2.aspx. In the IIS default document settings I have added the two pages and made Home1.aspx be the first default document then Home2.aspx the second. I need in some cases to be able to change the order of the two default documents so that Home2.aspx is the first default document then Home1.aspx the second.

How can I do that from my C# code?

Thanks in advance for any response

+1  A: 

one possibility is to have a DEFAULT or HOME page, which determines (based on the request) if the user should be sent to Home1 or Home2.

Stephen Wrighton
No i don't want to redirect according to the request, i need admin from back-end to determine the default page so according to his determination i need to set the default page by swapping the Home1.aspx to preceed Home.aspx2 if Home2.aspx is the default and vice versa.So how can i do that ? is this possible ?
Ahmy
+2  A: 

This Article shows you how to modify the IIS metabase in c# to do what you want.

You will have to enumerate through all of the properties to find the one you want. This article will help you with that.

AaronS
+2  A: 

This simple example demonstrates how to change the default document order:

using System.DirectoryServices;

class Program
{
    static void Main(string[] args)
    {
        // You need to change this value to match your site ID in IIS.
        int iisNumber = 668;  

        /* If your site is in its own IIS application/vdir under the site root
           and you've touched the default document settings or only want the 
           default document altered for that application/vdir folder then 
           specify as:

           IIS://Localhost/W3SVC/{0}/root/MyApplication
        */
        string metabasePath = 
               String.Format("IIS://Localhost/W3SVC/{0}/root", iisNumber);
        //Change one way
        using (DirectoryEntry de = new DirectoryEntry(metabasePath))
        {
            de.Properties["DefaultDoc"].Value = "Home1.aspx,Home2.aspx";
            de.CommitChanges();
        }

        // Change back
        using (DirectoryEntry de = new DirectoryEntry(metabasePath))
        {
            de.Properties["DefaultDoc"].Value = "Home2.aspx,Home1.aspx";
            de.CommitChanges();
        }
    }
}

This will work on IIS 6 and IIS 7 running the IIS 6 Management Compatibility bits.

Kev
How can i know my Site ID in IIS as i am testing localy now ? and how i will know it when the site is published ?thanks for your help too much
Ahmy
Ohhhhhh!!!!!Opps.... I have IIS 5 so how can i do that ?+1 for your great effort
Ahmy
Thanks a lot Mr.Kev your answer was helpful and i can change now the document order but i hav a small problem that when i run the application from and any directory it changes correctly but when i run the application from the IIS and try to change it reports an error saying : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) So Can you help me in this issue please? Thanks in advance
Ahmy
@Ahmy - the most likely reason for the error is that you need Administrator rights to modify the IIS config. The anonymous account for the site won't have these.
Kev
I have gave your the acceptance points so where is my points for this subjective question :D
Ahmy
A: 

Great post, thanks a lot!!!

Ran Kenig