ImageResizer无法在重写事件中应用预设(已经扩展的预设)

时间:2015-03-08 03:32:23

标签: c# asp.net imageresizer

我需要能够分析所请求的图像URL并根据请求的URL应用调整大小命令,而无需用户在URL中看到调整大小命令。 E.g。

http://someurl.com/image/category/image-title.jpg

要做到这一点,我已经连接到Pipeline_Rewrite - 下面的基本示例:

    void Pipeline_Rewrite(IHttpModule sender, HttpContext context, ImageResizer.Configuration.IUrlEventArgs e)
    {
        // Clear the current command string - we're going to create a new one
        e.QueryString.Clear();

        var process = ((Global)HttpContext.Current.ApplicationInstance).ContainerProvider.RequestLifetime.Resolve<Interfaces.IRequestBuilder>();
        // Set the "new" command string
        e.QueryString = process.BuildCommandList(context);            
    }

BuildCommandList(context)分析当前的url并构造一个适当的命令列表以添加到查询字符串,并返回NameValueCollection。这一切都正常,并且应用命令,除非新命令是在配置中定义的Preset

E.g。

如果BuildCommandList(context)返回等效的width=150&height=150,则可行。

如果BuildCommandList(context)返回相当于preset=thumbnail这不起作用,我只会获得完整尺寸的原始图像。

在进一步调查中,看起来任何Preset命令在Pipeline_Rewrite方法运行之前已经扩展到相关的命令字符串,因此在这里添加它们为时已晚。

从web.config文件中提取:

<resizer>
   <presets onlyAllowPresets="false">
      <preset name="thumbnail" settings="width=150;height=150" />
   </presets>
   <plugins>
      <add name="Presets" />
   </plugins>
</resizer>

有没有办法清除命令字符串,应用预设,并让ImageResizer在此时重新处理预设?

1 个答案:

答案 0 :(得分:0)

如果您希望Presets与事件处理程序一起使用,则需要首先注册事件处理程序。

我建议您从web.config中删除<add name="Presets" />,然后在注册事件处理程序后通过new ImageResizer.Plugins.Basic.Presets().Install(Config.Current);

安装它