从模型访问图像

时间:2016-03-09 15:49:50

标签: piranha-cms

我有一个ImageRegion,可以在我的App_Data中看到Image Guid文件。从我的Razor或Controller中检索图像的方法是什么。

我的ViewModel中有一个嵌入式页面模型;

public Piranha.Models.PageModel CMSData { get; set; }

并在我的控制器中填充

PhotoWeb.Models.LoginViewModel model = new Models.LoginViewModel();
model.CMSData = Piranha.Models.PageModel.GetByPermalink("welcome");

我可以使用Razor中的扩展对象看到相关的ImageRegion

@Model.CMSData.Regions.Header

这给了我一个ImageRegion,其中包含存储在App_Data \ Content中的图像文件的ID。由于安全原因无法通过src标记访问App_Data,因此访问图像并在<img>标记中显示该图像的正确Piranha方法是什么?

1 个答案:

答案 0 :(得分:0)

事实证明我需要使用“media.ashx”从@Model.CMSData.Regions.Header取回Guid前缀,如下所示。

/// <summary>
/// Replace the passed HTML content with one held in the CMS. If no match found
/// then the current content would be returned.
/// </summary>
public static string ReplaceImage(string current, 
    Func<Piranha.Extend.Regions.ImageRegion> cmsAccessor)
{
    Piranha.Extend.Regions.ImageRegion replacement = cmsAccessor();
    if (replacement == null)
    {
       return  current ;
    }
    return "media.ashx/" + replacement.Id.ToString();
}