我可以使用以下代码在我的 IIS 中创建一个 http 重定向。但是,它只会为主站点“MySite”本身创建重定向。如何以编程方式将重定向添加到虚拟目录?创建 VD 时是否需要设置一些设置?谢谢!
ServerManager iisManager = new ServerManager();
Configuration config = iisManager.GetWebConfiguration("MySite");
ConfigurationSection httpRedirectSection = config.GetSection("system.webServer/httpRedirect");
httpRedirectSection["enabled"] = true;
httpRedirectSection["destination"] = @"http://www.google.com";
httpRedirectSection["exactDestination"] = false;
httpRedirectSection["httpResponseStatus"] = @"Found";
iisManager.CommitChanges();
最佳答案
肖恩,
您的代码实际上是在修改您站点的 web.config 文件。虚拟目录可能被配置为一个应用程序,因此它会有自己的 web.config 文件。您是否尝试过做同样的事情但只是改变:
Configuration config = iisManager.GetWebConfiguration("MySite/VirtDirName");
另外,虚拟目录,因为它是一个子应用程序,可能已经从父站点继承了 httpRedirect 设置,我会首先检查这不是这种情况。
http://www.iis.net/ConfigReference/system.webServer/httpRedirect
关于c# - 如何使用 C# 从虚拟目录创建 HTTP 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219735/