2011-03-14 121 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(); 
     }