Sitecore Glass Mapper (v4): Add custom query string parameter via RenderImage

◇◆丶佛笑我妖孽 提交于 2020-01-14 06:03:49

问题


Using Glass Mapper for Sitecore version 4 (MVC), I need the following method call:

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

to result in:

<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" />

Currently, the RenderImage call will result in the cropimage being added as an attribute rather than on the query string of the image. There seems to be a list of values that, if passed in RenderImage, will be added as query string parameters rather than attributes, and this list seems to be defined in the main Config class:

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

So, my question is: what's the best way for me to add "cropregion" to this image parameter key hashset? Should I write my own Config file and substitute it for the stock Glass Mapper config class? Or is there some way for me to insert another value into this HashSet when Glass is initialized?


回答1:


I think you should be able to just add a single line to your GlassMapperScCustom.CreateResolver method. Try this:

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;
}


来源:https://stackoverflow.com/questions/34659740/sitecore-glass-mapper-v4-add-custom-query-string-parameter-via-renderimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!