[英]How to create an HTTP redirect from a virtual directory using C#
我可以使用以下代碼在我的 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();
如果您將其托管在 IIS 7+ 中,那么您可以使用web.config
。
在目錄下放置一個web.config
然后添加
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="/[PathFromRoot]/" childOnly="true" httpResponseStatus="Temporary" />
</system.webServer>
</configuration>
您可以在此處閱讀屬性
肖恩
您的代碼所做的實際上是為您的站點修改 web.config 文件。 虛擬目錄可能被配置為應用程序,因此它會有自己的 web.config 文件。 你有沒有嘗試做同樣的事情,只是簡單地改變:
Configuration config = iisManager.GetWebConfiguration("MySite/VirtDirName");
此外,虛擬目錄,因為它是一個子應用程序,可能已經從父站點繼承了 httpRedirect 設置,我首先檢查一下情況是否如此。
http://www.iis.net/ConfigReference/system.webServer/httpRedirect
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.