Equivalent of Request.Uri in Macro?

Jake Burgy asked on January 14, 2015 23:05

In a typical ASPX page, you can retrieve the currently requested URL with Request.Url.

I want to display the URL that the user tried to go to in error on a custom 404 page.

So I have a page in my document tree at /Special Pages/404 Not Found, with an Editable Text web part.

If I browse to http://mysite.com/invalid/url, on my 404 page, I want to say:

"You tried to access /invalid/url, but it wasn't found."

How can I get "/invalid/url" using a macro expression on the page?

Recent Answers


Virgil Carroll answered on January 14, 2015 23:44

You should be able to inherit the System.Net class in your macro and access Request.Uri from there. I have not tried it, but that should work.

If you are doing this in a transformation, I would probably not do a macro at all but instead just do some JavaScript or jQuery to get the referrer...Macors are good but have a performance hit to them. Might not be a big deal, but neither would,the client script.

Lastly, I would explore the CMS.Helper class, I feel like I have seen a function to be able to grab this, but I am away from my computer right now. Others might chime in.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 15, 2015 04:20 (last edited on December 10, 2019 02:30)

Have you tried {% CurrentDocument.NodeAliasPath |(identity)GlobalAdministrator%}

The first may bring up the document you're navigating to vs the error/404 page, haven't fully tested that yet though.

0 votesVote for this answer Mark as a Correct answer

Jake Burgy answered on January 15, 2015 14:57 (last edited on December 10, 2019 02:30)

Brenden,

Yes, the first brings up the 404 page's path (/Special-Pages/404), and the second property is empty (or null).

Virgil,

The Helpers namespace does have a few things that should be working ... but the Helpers namespace is not resolving in my macro at all. I am using the expression on an editable text region on a page. Here is the debug information for one of my attempts:

{% CMS.Helpers.RequestContext.CurrentURL |(identity)GlobalAdministrator%} null Jake CMSPagePlaceholder.LoadRegionsContent
> CMS.Helpers.RequestContext.CurrentURL null
>> CMS.Helpers.RequestContext null
>>> CMS.Helpers null
>>>> CMS null
0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on January 15, 2015 20:44 (last edited on December 10, 2019 02:30)

Jake,

You would probably have to create your own macro via code vs. use the class in a general macro statement. They are pretty easy to create and then you can either extend an existing namespace or create your own, so you would access it as {% MyNamespace.MyMacro |(identity)GlobalAdministrator%}

Check this out: https://docs.kentico.com/display/K8/Registering+custom+macro+methods

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on January 15, 2015 22:57 (last edited on January 15, 2015 23:22)

Implementation of the custom macro is the correct approach. But there is another, which is faster and does not require source code modification, but is not correct from architecture stand point. You may simply put editable text on your page, open settings dialog, go to Layout tab and alter (create new) layout, which will be the same as default + following:

<asp:Label ID="lblText" runat="server"/>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

`{        lblText.Text = Request.UrlReferrer.AbsoluteUri;    }`

</script>

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.