【问题标题】:Access Image from Model从模型访问图像
【发布时间】:2016-03-09 15:49:49
【问题描述】:

我有一个 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 方法是什么?

【问题讨论】:

    标签: piranha-cms


    【解决方案1】:

    事实证明,我需要在从@Model.CMSData.Regions.Header 返回的 Guid 前面加上“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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-15
      • 2017-08-18
      • 2013-02-25
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      相关资源
      最近更新 更多