title: How to locate image resources in Orchard
body: <p>I want to get a link to image resource in a MVC view that is part of an Orchard module. </p>
  
  <p>Googling a bit resulted in the following approach:</p>
  
  <p><a href="https://stackoverflow.com/a/9256515/3936440">https://stackoverflow.com/a/9256515/3936440</a></p>
  
  <p>and its using <code>@Html.ResourceUrl()</code> in views to get the resource URL. </p>
  
  <p>I wonder where the <code>ResourceUrl()</code> comes from as its not documented in MSDN and i cannot use it in my projects either. </p>
  
  <p>Did someone used that approach already and can shed some light on whats missing here?</p>
  
  <p>Update:</p>
  
  <p>I figured it out. The following code works in connection with Orchard modules.</p>
  
  <p>First you need to add a resource manifest to the Orchard module like so</p>
  
  <pre><code>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");
    }
  }
  </code></pre>
  
  <p>Then you extend the Html object:</p>
  
  <pre><code>// 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&lt;Orchard.UI.Resources.IResourceManager&gt;();
      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;
    }
  }
  </code></pre>
  
  <p>and then you can write: </p>
  
  <pre><code>&lt;img src="@Html.ResourceUrl("ProfilePicture", "User1")" /&gt;
  </code></pre>
  
  <p>in an Orchard module view to get appropriate image link for User1.</p>
  
  <p>I hope this helps.</p>