当我处于备用 View 时,我知道我可以使用以下方法获取 ContentItem 的显示 URL:
@Url.ItemDisplayUrl(contentItem)
但我不知道如何在 Controller 上下文 (ActionResult) 中获取显示 URL。出于站点地图的目的,我需要列出的 ContentItems 的 URL。
我正在使用下面的代码。
public class SiteMapResult : ActionResult
{
readonly IContentManager _contentManager;
readonly ITagService _tagService;
public SiteMapXmlResult(IContentManager contentManager, ITagService tagService)
{
_contentManager = contentManager;
_tagService = tagService;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "text/xml";
string host = context.HttpContext.Request.Url.Host;
StringBuilder xml = new StringBuilder();
xml.Append(@"<?xml version=""1.0"" encoding=""UTF-8""?>");
xml.Append(@"<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">");
var contentItems = _contentManager.Query(VersionOptions.Latest, GetContentTypeNames().ToArray()).List();
foreach (var contentItem in contentItems)
{
// The display url of contentItem
}
...
}
}
如何获取 ContentItem 显示 URL?
最佳答案
您确实可以通过 Url 属性从 Controller 访问 Url 助手。因此,只要您不要忘记在 Controller 文件之上使用 Orchard.Mvc.Html
,您就可以使用 Url.ItemDisplayUrl。
关于url - 获取 Orchard ContentItem 显示 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16862904/