Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • hlukey 2 posts 32 karma points
    Nov 30, 2023 @ 13:16
    hlukey
    0

    Retrieve Current Page

    This seems like it should be a pretty obvious question, and others have asked this, but their solution doesn't seem to work for me.

    On a given page, I want to change a style in the template based on which page is currently active.

    In Umbraco 8, I used the following code, which worked brilliantly:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{ 
     var currentPageURL = Url.Action()+"/";
    
    }
    

    In Umbraco 11, the same code gives me an empty string.

    The following code seems to be recommended:

    @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage
    @{ 
      var currentPageURL = Umbraco.AssignedContentItem;
    }
    

    However, the result is "UMBRACO.CMS.WEB.COMMON.PUBLISHEDMODELS.SITECONTENT" whereas I want to know the actual value.

    Are there any suggestions on what I need to do to correct this? Thanks! Luke

  • Garðar Þorsteinsson 119 posts 566 karma points
    Nov 30, 2023 @ 13:20
    Garðar Þorsteinsson
    101

    Hi,

    var currentPageURL = Umbraco.AssignedContentItem;

    Gives you the object not url of the page.

    You will need to ask for the url like this:

    var currentPageURL = Umbraco.AssignedContentItem.Url();

    You should also be able to do

    var currentPageURL = Model.Url();

  • hlukey 2 posts 32 karma points
    Nov 30, 2023 @ 13:24
    hlukey
    0

    Brilliant. Thank-you!!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies