获取Orchard ContentItem显示URL

ste*_*urt 3 sitemap url controller routes orchardcms

当我在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

        }

        ...

    }
}
Run Code Online (Sandbox Code Playgroud)

如何获取ContentItem显示URL?

Ber*_*Roy 6

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