是否可以从 Azure ServiceConfiguration 文件而不是 web.config 读取 IIS 重写规则?
出现的问题是,我们对某些每周更新的内容管理页面有友好的 URL,因此每周都会创建一个新的 URL。旧的存储在新闻列表存档中,因此无法覆盖。
我们希望尝试避免每周上传 Azure 站点文件,并希望能够通过更改 serviceconfig 中的值来快速(立即)响应可能的链接更改。
有人知道这是否可行或是否有其他解决方案?
谢谢
最佳答案
是的,您可以使用 IIS 管理 API 中的配置编辑器类来更改您的角色以在运行时修改 web.config。我还没有尝试过这个,但它应该使您能够在启动期间从 Azure 配置加载设置,然后应用于您的角色的运行时实例。因此,您可以在网络角色的 global.asax 的 Application_start 部分中进行设置。
或者,您可以使用启动任务在角色启动时以编程方式构建 web.config。
对于第一种方法:
在 iis.net 上做一些研究,然后阅读此 IIS 论坛帖子: http://forums.iis.net/t/1150481.aspx
从用户 ruslany 处获取示例(在适当的地方给予信用,但粘贴以便您可以看到它):
using(ServerManager serverManager = new ServerManager()) {
Configuration config = serverManager.GetWebConfiguration("Default Web Site");
ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");
ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();
ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
ruleElement["name"] = @"MyTestRule";
ruleElement["stopProcessing"] = true;
ConfigurationElement matchElement = ruleElement.GetChildElement("match");
matchElement["url"] = @"foo\.asp";
ConfigurationElement conditionsElement = ruleElement.GetChildElement("conditions");
ConfigurationElementCollection conditionsCollection = conditionsElement.GetCollection();
ConfigurationElement addElement = conditionsCollection.CreateElement("add");
addElement["input"] = @"{HTTP_HOST}";
addElement["pattern"] = @"www\.foo\.com";
conditionsCollection.Add(addElement);
ConfigurationElement actionElement = ruleElement.GetChildElement("action");
actionElement["type"] = @"Rewrite";
actionElement["url"] = @"bar.asp";
rulesCollection.Add(ruleElement);
serverManager.CommitChanges();
}
关于iis-7 - 是否可以从 Azure ServiceConfiguration 文件读取 IIS 重写规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5299197/