【问题标题】:How to locate image resources in OrchardOrchard中如何定位图片资源
【发布时间】:2015-04-16 22:55:21
【问题描述】:

我想在作为 Orchard 模块的一部分的 MVC 视图中获取图像资源的链接。

谷歌搜索了一下,得到了以下方法:

https://stackoverflow.com/a/9256515/3936440

它在视图中使用@Html.ResourceUrl() 来获取资源URL。

我想知道 ResourceUrl() 来自哪里,因为它没有在 MSDN 中记录,我也不能在我的项目中使用它。

是否有人已经使用过这种方法并且可以阐明这里缺少什么?

更新:

我想通了。以下代码与 Orchard 模块结合使用。

首先你需要像这样向 Orchard 模块添加一个资源清单

public class ResourceManifest : Orchard.UI.Resources.IResourceManifestProvider
{
  public void BuildManifests(Orchard.UI.Resources.ResourceManifestBuilder aBuilder)
  {
    Orchard.UI.Resources.ResourceManifest lManifest = aBuilder.Add();

    string lModulePath = "~/Modules/YourModuleName";

    lManifest.DefineResource("ProfilePicture", "User1").SetUrl(lModulePath + "/Images/User1.png");
  }
}

然后你扩展 Html 对象:

// This class adds so called "extension methods" to class System.Web.Mvc.HtmlHelper
public static class HtmlHelperExtensions
{
  // This method retrieves the URL of a resource defined in ResourceManifest.cs via the Orchard resource management system
  public static string ResourceUrl(this System.Web.Mvc.HtmlHelper aHtmlHelper, string aResourceType, string aResourceName)
  {
    // note:
    //  resolving Orchard.UI.Resources.IResourceManager via work context of orchard because
    //    - calling System.Web.Mvc.DependencyResolver.Current.GetService() does not work as it always returns null at this point
    //    - constructor parameter injection is not allowed in static classes
    //    - setting the resource manager from another class that uses constructor parameter injection does not work as it causes a "circular component dependency "
    Orchard.WorkContext lWorkContext = Orchard.Mvc.Html.HtmlHelperExtensions.GetWorkContext(aHtmlHelper);

    Orchard.UI.Resources.IResourceManager lResourceManager = (Orchard.UI.Resources.IResourceManager)lWorkContext.Resolve<Orchard.UI.Resources.IResourceManager>();
    if (lResourceManager != null)
    {
      Orchard.UI.Resources.RequireSettings lSettings = new Orchard.UI.Resources.RequireSettings { Type = aResourceType, Name = aResourceName, BasePath = aResourceType };

      Orchard.UI.Resources.ResourceDefinition lResource = lResourceManager.FindResource(lSettings);
      if (lResource != null)
      {
        Orchard.UI.Resources.ResourceRequiredContext lContext = new Orchard.UI.Resources.ResourceRequiredContext { Resource = lResource, Settings = lSettings };

        string lAppBasePath = System.Web.HttpRuntime.AppDomainAppVirtualPath;

        return lContext.GetResourceUrl(lSettings, lAppBasePath);
      }
    }

    return null;
  }
}

然后你可以写:

<img src="@Html.ResourceUrl("ProfilePicture", "User1")" />

在 Orchard 模块视图中为 User1 获取适当的图像链接。

我希望这会有所帮助。

【问题讨论】:

  • 不,初读时没看懂作者写了什么,ResourceUrl() 是System.Web.Mvc.HtmlHelper 类的所谓扩展方法。问题解决了。
  • 这看起来有点矫枉过正。把图片放到模块的Content/Images文件夹,然后用Url.Content有什么问题?
  • @Bertrand Le Roy 按照您的建议将图像简单地放在一个文件夹中不适用于我为模块提供的路线。因此,我想出了集中资源的方法来避免一遍又一遍地编写 ~/Modules/YourModuleName/Images/MyImage。
  • 怎么不行?注册资源用于必须跨模块重用的资源,而不是简化路由解析。

标签: c# asp.net-mvc image url orchardcms


【解决方案1】:

ResourceUrl() 是自定义的HtmlHelper 扩展。

您需要实现它的代码包含在您链接的答案中。

您只需要创建一个包含方法代码的静态类。

Asp.net article on how to create custom html helpers

PS:确保您使用@using YourNamespace 将您的命名空间导入到视图中,或者将其添加到System.Web.Mvc.HtmlHelper 类中。

【讨论】:

  • 是的,一切都正确,但在果园并不那么容易。我用在 Orchard 模块中工作所需的代码更新了我的问题。
  • @ViRuSTRiNiTy 不错。 :)
猜你喜欢
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 2011-09-12
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
相关资源
最近更新 更多