是否可以从Azure ServiceConfiguration文件而不是web.config中读取IIS重写规则?
出现的问题是我们对内容管理的某些每周更新页面有友好的网址,因此每周都会创建一个新的网址。旧的存储在新闻列表存档中,因此不能选择覆盖。
我们希望尝试避免每周上传Azure站点文件,并希望能够通过更改serviceconfig中的值来快速(立即)响应可能的链接更改。
任何人都知道这是否可行,还是有另一种解决方案?
由于
答案 0 :(得分:1)
是的,您可以使用IIS Admin api中的配置编辑器类在运行时更改角色以修改web.config。我没有尝试过这个,但它应该允许您在启动期间从Azure配置加载设置,然后应用于您的角色的运行时实例。因此,您可以在Web角色的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();
}