How do I change the Default Document order programmatically in IIS using C#?
I have an ASP.NET webs开发者_如何学Goite 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
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.
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.
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.
精彩评论