c# - 如何使用 C# 从虚拟目录创建 HTTP 重定向

标签 c# iis redirect directory virtual

我可以使用以下代码在我的 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

http://msdn.microsoft.com/en-us/library/ms178685.aspx

关于c# - 如何使用 C# 从虚拟目录创建 HTTP 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219735/

相关文章:

asp.net-core - 如何在 IIS 上运行的 aspnet core 应用程序的 appsettings.json 中配置 IdentityServer 的关键设置

java - 在 JasperException 上重定向页面

c - linux系统c引用

c# - 我如何在 Linq 中分组?

c# - 动态调整表格单元格宽度

c# - 具有多种条件的下拉列表 - 从数据库中选择

azure - 发布完成后 IIS 物理路径发生变化 Azure DevOps

WCF 服务端点与主机基地址

c# - 如何制作可插入的静态类

redirect - 在 Jekyll 和 GitHub Pages 中重定向旧页面的最佳方法是什么?