2017-08-04 103 views
0

我想以編程方式設置配置參數clientcache.minutes,但我正在與ImageResizer中的配置設計掙扎。如何以編程方式設置ImageResizer配置?

我的做法是目前:

var lWebConfigReader = new System.Xml.XmlTextReader(@"Web.config"); 

var lXmlDocument = new System.Xml.XmlDocument(); 
lXmlDocument.Load(lWebConfigReader); 

var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer"); 

var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml); 

var lConfig = new ImageResizer.Configuration.Config(lSection); 

int mins = lConfig.get("clientcache.minutes", -1); 

... 

ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml()); 

這似乎有點哈克,也因爲它通常應在clientcache.minutes被設置ClientCache插件不發送Expires頭不起作用。

可能是什麼問題?

回答

0

經過對源代碼的挖掘後,我發現在這種特殊情況下,您需要更改全局配置對象,因爲ClientCache插件通過Get()從中讀取參數。所以我目前的解決方案是:

// read a XML where a <resizer>...</resizer> is present, in this case a typical Web.config as mentioned in the ImageResizer docs 
var lWebConfigReader = new System.Xml.XmlTextReader(@"Web.config"); 

var lXmlDocument = new System.Xml.XmlDocument(); 
lXmlDocument.Load(lWebConfigReader); 

// read the resizer tag to a node 
var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer"); 

// create a section from the node 
var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml); 

// create a new config object from the section 
var lConfig = new ImageResizer.Configuration.Config(lSection); 

// override the global configugration with the newly created one 
ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml()); 

// test the Get() call used by the ClientCache plugin 
int mins = ImageResizer.Configuration.Config.Current.get("clientcache.minutes", -1); 

該代碼可以被放置在一個ICurrentConfigProvider實施或Global.asaxApplication_Start()

相關問題