[英]How to set ImageResizer configuration programmatically?
I want to set the configuration parameter clientcache.minutes
programmatically but im struggling with the config design in ImageResizer. 我想以编程方式设置配置参数
clientcache.minutes
但在ImageResizer中的配置设计上clientcache.minutes
。
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. 似乎有点骇人听闻,并且也不起作用,因为
ClientCache
插件没有像设置clientcache.minutes
时通常发送的那样发送Expires
标头。
What could be the issue? 可能是什么问题?
After some digging in the source code i found out that in this particular case you need to alter the global configuration object as the ClientCache
plugin reads the parameter via Get()
from it. 在仔细研究了源代码之后,我发现在这种特殊情况下,您需要更改全局配置对象,因为
ClientCache
插件会通过Get()
从中读取参数。 So my current solution is: 所以我目前的解决方案是:
// 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);
This code could be placed in a ICurrentConfigProvider
implementation or Application_Start()
in Global.asax
. 可以将这些代码放在
ICurrentConfigProvider
实现或Global.asax
Application_Start()
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.