How to set ImageResizer configuration programmatically?

时间:2017-08-04 13:12:10

标签: c# config imageresizer

I want to set the configuration parameter clientcache.minutesprogrammatically but im struggling with the config design in ImageResizer.

My approach currently is:

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());

It seems a bit hacky and also doesn't work as the ClientCache plugin doesn't sent the Expires header as it normally should when clientcache.minutes is set.

What could be the issue?

1 个答案:

答案 0 :(得分: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实施中,也可以放在Application_Start()中的Global.asax