Skip to content

Razor 语法帮助器

在Razor中,有许多扩展方法可用于@Orchard

Razor扩展

方法 模块 描述
DisplayAsync(ContentItem content, string displayType = "") OrchardCore.ContentManagement.Display 使用相应的显示类型呈现内容项。
GetContentCultureAsync(ContentItem contentItem) OrchardCore.ContentLocalization 返回给定ContentItem的文化。
CultureDir() OrchardCore.DisplayManagement 返回当前文化方向。
CultureName() OrchardCore.DisplayManagement 返回当前文化名称。
ResourceUrl(string resourcePath, bool? appendVersion = null) OrchardCore.ResourceManagement 将Cdn Base URL前缀添加到指定的资源路径。
GetContentItemIdByAliasAsync(string alias) OrchardCore.Alias 从其别名返回内容项ID。例如:carousel
GetContentItemIdBySlugAsync(string slug) OrchardCore.Autoroute 从其slug返回内容项ID。例如:myblog/my-blog-post
GetContentItemIdByHandleAsync(string handle) OrchardCore.Contents 从其句柄返回内容项ID。例如:alias:carouselslug:myblog/my-blog-post
GetContentItemByAliasAsync(string alias, bool latest = false) OrchardCore.Alias 通过其别名加载内容项,寻找最新版本或不寻找。例如:carousel
GetContentItemBySlugAsync(string slug, bool latest = false) OrchardCore.Autoroute 通过其slug加载内容项,寻找最新版本或不寻找。例如:slug:myblog/my-blog-post
GetContentItemByHandleAsync(string handle, bool latest = false) OrchardCore.Contents 通过其句柄加载内容项,寻找最新版本或不寻找。例如:alias:carouselslug:myblog/my-blog-post
GetContentItemByIdAsync(string contentItemId, bool latest = false) OrchardCore.Contents 通过其ID加载内容项。
GetContentItemsByIdAsync(IEnumerable<string> contentItemIds, bool latest = false) OrchardCore.Contents 通过其ID加载一组内容项。
GetContentItemByVersionIdAsync(string contentItemVersionId) OrchardCore.Contents 通过其版本ID加载内容项。
QueryContentItemsAsync(Func<IQuery<ContentItem, ContentItemIndex>, IQuery<ContentItem>> query) OrchardCore.Contents 查询内容项。
GetRecentContentItemsByContentTypeAsync(string contentType, int maxContentItems = 10) OrchardCore.Contents 加载特定类型的内容项。
LiquidToHtmlAsync(string liquid) OrchardCore.Liquid 将liquid字符串解析为HTML。
LiquidToHtmlAsync(string liquid, object model) OrchardCore.Liquid 将liquid字符串解析为HTML。
SanitizeHtml(string html) OrchardCore.Infrastructure 清理HTML字符串。
QueryListItemsCountAsync(string listContentItemId, Expression<Func<ContentItemIndex, bool>> itemPredicate = null) OrchardCore.Lists 返回列表计数。
QueryListItemsAsync(string listContentItemId, Expression<Func<ContentItemIndex, bool>> itemPredicate = null) OrchardCore.List 返回列表项。
MarkdownToHtmlAsync(string listContentItemId, Expression<Func<ContentItemIndex, bool>> itemPredicate = null) OrchardCore.Markdown 将Markdown字符串转换为HTML。
AssetUrl(string assetPath, int? width = null, int? height = null, ResizeMode resizeMode = ResizeMode.Undefined, bool appendVersion = false) OrchardCore.Media 返回具有可选调整大小参数的指定资产路径的相对URL。
ImageResizeUrl(string imagePath, int? width = null, int? height = null, ResizeMode resizeMode = ResizeMode.Undefined) OrchardCore.Media 返回具有自定义调整大小参数的现有图像路径的URL。
ContentQueryAsync(string queryName) OrchardCore.Queries 返回Content项列表
ContentQueryAsync(string queryName, IDictionary<string, object> parameters) OrchardCore.Queries 返回Content项列表
QueryAsync(string liquid, object model) OrchardCore.Queries 返回对象列表
QueryAsync(string queryName, IDictionary<string, object> parameters) OrchardCore.Queries 返回对象列表
ShortcodesToHtmlAsync(string html, object model = null) OrchardCore.Shortcodes 渲染短代码。
GetTaxonomyTermAsync(string taxonomyContentItemId, string termContentItemId) OrchardCore.Taxonomies 从其内容项ID和分类法返回术语。
GetInheritedTermsAsync(string taxonomyContentItemId, string termContentItemId) OrchardCore.Taxonomies 返回包括其父项在内的术语列表。
QueryCategorizedContentItemsAsync(string taxonomy(Func<IQuery<ContentItem, TaxonomyIndex>, IQuery<ContentItem>> query) OrchardCore.Taxonomies 查询内容项。

如何使用

如果您想在视图中使用扩展方法,可以在文件顶部注入名为OrchardIOrchardHelper

@inject OrchardCore.IOrchardHelper Orchard

OrchardCore.DisplayManagement.Razor中,有一个RazorPage,它已经有一个名为Orchard的公共属性,您可以使用它来调用扩展方法或当前的HttpContext

如果您想在控制器中使用Orchard助手,可以在构造函数中注入一个实例:

private IOrchardHelper _orchard;

public MyClass(IOrchardHelper orchard)
{
    _orchard = orchard;
}

注意

如果您想要使用的扩展方法找不到(例如在主题中),请不要忘记引用相应的模块。

该文档由ChatGPT 4 翻译


Last update: April 26, 2023