是的,你可以改變你的角色在IIS管理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();
}