0

我有一个 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。因为出于安全原因,App_Data 不能通过 src 标签访问,所以访问图像并将其显示在我的<img>标签中的正确 Piranha 方法是什么?

4

1 回答 1

0

事实证明,我需要在返回的 Guid 前面@Model.CMSData.Regions.Header加上“media.ashx”,如下所示。

/// <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();
}
于 2016-03-11T10:07:24.020 回答