More in this section

Forums / Developing with Sitefinity / Retrieving page URLs in a multilingual website

Retrieving page URLs in a multilingual website

4 posts, 0 answered
  1. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    28 Nov 2012
    Link to this post
    Hi,

    I'm trying to understand how to retrieve full (absolute, not relative, not starting with ~/) page URLs in a multilingual website. Sitefinity knows about them. All I want is get the URL Sitefinity is using for a page.

    In case I have the page id (Me.LinkPageGUID in the code below), this is what I've come up with:

    Dim LinkPage As PageNode = App.WorkWith.Pages.LocatedIn(Fluent.Pages.PageLocation.Frontend).Where(Function(p) p.Id = Me.LinkPageGUID).Get.FirstOrDefault()
     
    Dim FullDomain As String = Page.Request.Url.Scheme & System.Uri.SchemeDelimiter & Page.Request.Url.Host & (If(Page.Request.Url.IsDefaultPort, "", ":" & Page.Request.Url.Port))
    Dim PageUrl As String = Page.ResolveUrl(LinkPage.GetFullUrl(Threading.Thread.CurrentThread.CurrentUICulture, True))
    Me.Link1.NavigateUrl = FullDomain & PageUrl

    1) It works fine, but it seems a bit overcomplicated. Is there a cleaner way to achieve the same thing?

    2) I haven't figured this one out: From within a forum widget, in case I have the GUID of a forum, how do I get the URL of the page that has been configured to display the forum threads? I can get to the Forum object, but all it exposes is a DefaultPageId property. I'm not sure what to do with that, or if I need it at all. It seems to be the Id of the current page, which is not what I need.

    Thanks,
    Arno
  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    29 Nov 2012 in reply to Arno
    Link to this post
    Hi Arno,

    You could use this method to get a PageNode back:
    /// <summary>
    /// Get a page url
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public static string GetPageUrl(Guid id) {
       var result = string.Empty;
       var currentLanguage = Thread.CurrentThread.CurrentUICulture;
       var s = ObjectFactory.Resolve<UrlLocalizationService>();
       var pm = PageManager.GetManager();
       try {
          if (s != null) {
             var pageNode = pm.GetPageNode(id);
             if (pageNode != null) result = s.ResolvePageUrl(pageNode, currentLanguage);
          }
       } catch (ItemNotFoundException) {
           return result;
       } catch (NullReferenceException) {
           return result;
       }
       return result;
    }

    Then use the VirtualPathProvider to get an absolute URL, like this:
    var url = GetPageUrl(pageId);
    if (url == null) return;
    var absoluteUrl = VirtualPathUtility.ToAbsolute(url);

    Hope that works for you?

    Regards,
    Daniel
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    29 Nov 2012 in reply to Daniel Plomp
    Link to this post
    Sorry, I see you are using VB.NET.
    Then it would be like this:

    ''' <summary>
    ''' Get a page url
    ''' </summary>
    ''' <param name="id"></param>
    ''' <returns></returns>
    Public Shared Function GetPageUrl(id As Guid) As String
        Dim result = String.Empty
        Dim currentLanguage = Thread.CurrentThread.CurrentUICulture
        Dim s = ObjectFactory.Resolve(Of UrlLocalizationService)()
        Dim pm = PageManager.GetManager()
        Try
            If s IsNot Nothing Then
                Dim pageNode = pm.GetPageNode(id)
                If pageNode IsNot Nothing Then
                    result = s.ResolvePageUrl(pageNode, currentLanguage)
                End If
            End If
        Catch generatedExceptionName As ItemNotFoundException
            Return result
        Catch generatedExceptionName As NullReferenceException
            Return result
        End Try
        Return result
    End Function

    and this:
    Dim url = GetPageUrl(pageId)
    If url Is Nothing Then
        Return
    End If
    Dim absoluteUrl = VirtualPathUtility.ToAbsolute(url)

    Regards,
    Daniel
  4. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    29 Nov 2012 in reply to Daniel Plomp
    Link to this post
    Thanks a lot Daniel! That makes more sense to me than my LINQ attempt. And I decided to store the domain as an app setting in web.config and get it from there if I need it.

    Now, I hope someone knows how to get the forum page. Still haven't figured out that one.
4 posts, 0 answered