Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS 7 - Virtual directory redirection path?

I wrote this little web app that lists the websites running on the local IIS + virtual directories attached to the websites.

Using the following line I was able to get the HTTP Redirection URL of a virtual directory, if it was set to redirect:

_directoryEntry.Properties["HttpRedirect"].Value.toString()

Which works quite nicely in IIS 6 - but the value is empty when I try my app in an IIS 7 - and I tried switching the application pool to Classic pipeline as well - what has changed in IIS 7 here? And why?

like image 731
Dynde Avatar asked Jun 21 '11 10:06

Dynde


Video Answer


2 Answers

In IIS7 <httpRedirect> element replaces the IIS 6.0 HttpRedirect metabase property.

You need to set it up like this in your web.config file:

  <system.webServer> 
    <httpRedirect enabled="true" destination="WebSite/myDir/default.aspx" />" 
  </system.webServer> 

If you do not want to tweak web.config, this article talks about a way to do them the IIS 6 way: Creating Http Redirects in IIS7 on Virtual Directories like IIS6

Hope this helps.

like image 96
Mrchief Avatar answered Oct 19 '22 18:10

Mrchief


What has changed?: IIS7 has a completely new configuration system similar to .NET's hierarchical configuration system. Checkout this link for more detail here on what's changed.

How to get the HttpRedirect value: In C#, rather than using the System.DirectoryServices namespace to access the IIS configuration settings, use the new Microsoft.Web.Administration.dll.

Your code should look something like this example from IIS.net:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetWebConfiguration("Default Web Site");
         ConfigurationSection httpRedirectSection =                                  config.GetSection("system.webServer/httpRedirect");
         Console.WriteLine("Redirect is {0}.", httpRedirectSection["enabled"].Equals("true") ? "enabled" : "disabled");

      }
  }
}

You can actually do quite a lot with the new Microsoft.Web.Administration.dll. Checkout Carlos Ag's blog here for some ideas.

Two quick notes:

  • Microsoft.Web.Administration.dll is available if the "IIS Management Scripts and Tools" role service is installed. It should be under the inetsrv directory in systemroot.
  • Any code you run with the MWA dll needs to run as Administrator to access IIS configuration, so just make sure the account running the script has admin rights.

Hope this helps!

like image 41
mrdc Avatar answered Oct 19 '22 20:10

mrdc