如何在Orchard中定位图片资源
How to locate image resources in Orchard
我想在属于 Orchard 模块的 MVC 视图中获取 link 图像资源。
通过谷歌搜索得到以下方法:
及其在视图中使用 @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 获取适当的图像 link。
希望对您有所帮助。
ResourceUrl()
是自定义 HtmlHelper
扩展。
您需要实现它的代码包含在您链接的答案中。
您只需创建一个包含方法代码的静态 class。
Asp.net article on how to create custom html helpers
PS:确保使用 @using YourNamespace
将命名空间导入视图或将其添加到 System.Web.Mvc.HtmlHelper
class.
我想在属于 Orchard 模块的 MVC 视图中获取 link 图像资源。
通过谷歌搜索得到以下方法:
及其在视图中使用 @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 获取适当的图像 link。
希望对您有所帮助。
ResourceUrl()
是自定义 HtmlHelper
扩展。
您需要实现它的代码包含在您链接的答案中。
您只需创建一个包含方法代码的静态 class。
Asp.net article on how to create custom html helpers
PS:确保使用 @using YourNamespace
将命名空间导入视图或将其添加到 System.Web.Mvc.HtmlHelper
class.