Sitecore - 参数模板中的图像字段

Mar*_*ies 8 sitecore

如果我在参数模板中有一个图像字段,那么在c#中获取图像的URL有哪些步骤?

Ben*_*den 9

@mdresser提出了一个关于什么应该和不应该是渲染参数的有效点.但是,我认为Sitecore不会故意在参数模板中使用图像字段.他们只是在现有的键值对渲染参数功能上构建参数模板功能.

如果渲染参数模板上的图像字段名称为BackgroundImage,则可以使用以下代码获取所选图像的URL:

var imageId = XmlUtil.GetAttribute("mediaid", XmlUtil.LoadXml(this.Parameters["BackgroundImage"]));
MediaItem imageItem = Sitecore.Context.Database.GetItem(imageId);
backgroundImageUrl = MediaManager.GetMediaUrl(imageItem);
Run Code Online (Sandbox Code Playgroud)

如果您还没有提供Parameters属性的子布局的基类,则还需要添加以下内容:

private NameValueCollection parameters;
public virtual NameValueCollection Parameters
{
    get
    {
        if (this.parameters == null)
        {
            var parameters = this.Attributes["sc_parameters"];
            this.parameters = string.IsNullOrEmpty(parameters)
                                  ? new NameValueCollection()
                                  : WebUtil.ParseUrlParameters(parameters);
        }
        return this.parameters;
    }
}
Run Code Online (Sandbox Code Playgroud)