2011-03-14 124 views
1

是否可以从Azure ServiceConfiguration文件而不是web.config中读取IIS重写规则?是否可以从Azure ServiceConfiguration文件读取IIS重写规则?

出现的问题是,我们对每周更新的某些内容进行管理的网页具有友好的网址,因此每周都会创建一个新网址。旧的存储在新闻列表存档中,因此覆盖不是一种选择。

我们希望尽量避免每周上传Azure站点文件,并希望能够通过更改serviceconfig中的值来立即快速响应可能的链接更改。

任何人有任何想法,如果这是可能的或是否有其他解决方案?

感谢

回答

1

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