3

当我在 View Alternate 中时,我知道我可以使用以下方法获取 ContentItem 的显示 URL:

@Url.ItemDisplayUrl(contentItem)

但是当我在控制器上下文 (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?

4

1 回答 1

6

确实可以通过 Url 属性从控制器访问 Url 助手。因此,只要您不要忘记using Orchard.Mvc.Html在您的控制器文件之上,Url.ItemDisplayUrl 将可供您使用。

于 2013-05-31T20:03:13.547 回答