Sitecore Glass Mapper(v4):通过RenderImage添加自定义查询字符串参数

时间:2016-01-07 16:04:00

标签: sitecore sitecore8 glass-mapper

使用Glass Mapper for Sitecore版本4(MVC),我需要以下方法调用:

@Html.Glass().RenderImage(Model, x => x.PhotoField, new {cropregion = xmlValue.GetCropRegion()}, true, true)

导致:

<img src="/sitecore/shell/-/media/Default-Website/cover.jpg?h=550&amp;w=1600&amp;la=en&amp;hash=2986CA8291803D4A8EAC3B5A1C276E27D4877DCC&amp;cropregion=534,11,1348,548" width="1600" height="550" />

目前,RenderImage调用将导致cropimage被添加为属性而不是图像的查询字符串。似乎有一个值列表,如果在RenderImage中传递,将作为查询字符串参数而不是属性添加,并且此列表似乎在主Config类中定义:

            ImageQueryString = new HashSet<string>(new[]
        {
            ImageParameterKeys.OUTPUT_METHOD,
            ImageParameterKeys.ALLOW_STRETCH,
            ImageParameterKeys.IGNORE_ASPECT_RATIO,
            ImageParameterKeys.SCALE,
            ImageParameterKeys.MAX_WIDTH,
            ImageParameterKeys.MAX_HEIGHT,
            ImageParameterKeys.THUMBNAIL,
            ImageParameterKeys.BACKGROUND_COLOR,
            ImageParameterKeys.DATABASE,
            ImageParameterKeys.LANGUAGE,
            ImageParameterKeys.VERSION,
            ImageParameterKeys.DISABLE_MEDIA_CACHE,
            ImageParameterKeys.WIDTH,
            ImageParameterKeys.HEIGHT
        });

所以,我的问题是:什么是我添加&#34; cropregion&#34;的最佳方式。这个图像参数键hashset?我应该编写自己的Config文件并将其替换为Glass Mapper配置类库存吗?或者,在初始化Glass时,是否有某种方法可以在此HashSet中插入另一个值?

1 个答案:

答案 0 :(得分:1)

我认为您应该只能在GlassMapperScCustom.CreateResolver方法中添加一行。试试这个:

public static IDependencyResolver CreateResolver(){
    var config = new Glass.Mapper.Sc.Config();
    config.ImageQueryString.Add("cropregion");

    var dependencyResolver = new DependencyResolver(config);
    // add any changes to the standard resolver here
    return dependencyResolver;
}