【问题标题】:IIS 7 - Virtual directory redirection path?IIS 7 - 虚拟目录重定向路径?
【发布时间】:2011-09-19 09:40:22
【问题描述】:

我编写了这个小网络应用程序,它列出了在本地 IIS 上运行的网站以及附加到网站的虚拟目录。

如果设置为重定向,我可以使用以下行获取虚拟目录的 HTTP 重定向 URL:

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

这在 IIS 6 中工作得非常好——但是当我在 IIS 7 中尝试我的应用程序时,该值是空的——我也尝试将应用程序池切换到经典管道——这里的 IIS 7 发生了什么变化?为什么?

【问题讨论】:

    标签: iis iis-7 redirect virtual-directory


    【解决方案1】:

    在 IIS7 中 <httpRedirect> 元素替换 IIS 6.0 HttpRedirect 元数据库属性。

    你需要在你的web.config文件中这样设置它:

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

    如果您不想调整 web.config,本文讨论了一种通过 IIS 6 方式进行调整的方法:Creating Http Redirects in IIS7 on Virtual Directories like IIS6

    希望这会有所帮助。

    【讨论】:

    • 所以我没有简单的方法来使用代码(即c#)编辑/操作路径吗?它必须是 cmd 或 web.config?
    • 唯一改变的是如何配置 IIS。 IIS6 是通过元数据库完成的,IIS7 是通过 web.config 完成的。
    【解决方案2】:

    有什么变化?:IIS7 有一个全新的配置系统,类似于 .NET 的分层配置系统。查看此链接以获取更多详细信息here,了解更改内容。

    如何获取 HttpRedirect 值:在 C# 中,不要使用 System.DirectoryServices 命名空间来访问 IIS 配置设置,而是使用新的 Microsoft.Web.Administration.dll。

    您的代码应该类似于来自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");
    
          }
      }
    }
    

    您实际上可以使用新的 Microsoft.Web.Administration.dll 做很多事情。查看 Carlos Ag 的博客 here 了解一些想法。

    两个快速说明:

    • 如果安装了“IIS 管理脚本和工具”角色服务,则 Microsoft.Web.Administration.dll 可用。它应该在 systemroot 的 inetsrv 目录下。
    • 使用 MWA dll 运行的任何代码都需要以管理员身份运行才能访问 IIS 配置,因此请确保运行脚本的帐户具有管理员权限。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 1970-01-01
      • 2010-11-29
      • 2011-02-12
      • 1970-01-01
      • 2010-10-11
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多