Get current page URL

Topics: General, Troubleshooting, XSLT
Mar 10, 2011 at 5:23 AM

I was looking for some help.  I created my template and I need to be able to obtain the complete url of the page the user is on.  I use this in my tell a friend widget.  I pass the url value along with the email that was sent.  How can I set my template to capture the full url? 

Your help is greatly appricated.

Thanks

Mar 10, 2011 at 8:43 AM

    private static string GetRequestUrl()
    {
        var context = System.Web.HttpContext.Current;
        var urlBuilder = new Composite.Core.UrlBuilder(context.Request.RawUrl);
        urlBuilder.ServerUrl = new Composite.Core.UrlBuilder(context.Request.Url.ToString()).ServerUrl;

        return urlBuilder.ToString();
    }

Mar 10, 2011 at 1:27 PM

That seems a but complex, why do you need to involve Composite UrlBuilder in this? What about just

HttpContext.Current.Request.Url.ToString()
or if you don't want the url that was used to access the page, but the Composite's url to the page being rendered you can use
SitemapNavigator.CurrentPageNode.Url

Mar 10, 2011 at 2:37 PM
Edited Mar 10, 2011 at 2:38 PM

That's a good question

 If you use HttpContext.Current.Request.Url.ToString() you will get an internal url, that is a result of url-rewriting, something like /Renderers/Page.aspx?id={page Id}, which may be a valid solution, but I guess @c2w wants to see the actual url that were used in browser

SitemapNavigator.CurrentPageNode.Url - will be a page url, but without query string and PathInfo, it may be ok, but if there's somekind of url routing, it's not a fact that it will work. 

>> why do you need to involve Composite UrlBuilder in this? 

Because standard asp.net API for urls is just BAD. You can try read "Making Sense of ASP.NET Paths" to see that it has no sense at all :(, standard Uri class does not allow me even add or remove a query string parameter, and therefore completely useless in the code I usually have to write

That's the reason I have written and using UrlBuilder, since that the only way to be sure it is working right - to do it yourself :)


So my solution isn't a "one line solution" but I have enough confidence in to recommend it


Mar 10, 2011 at 3:15 PM

I am a bit new at using C1 and .Net.  In what file would I apply your code.  I created my template.  In this template I have the need to optain the URL of the current page the user is on to send as and email link.  Thanks for any help. I am really excited about C1.

Mar 10, 2011 at 3:27 PM

@c2w There is actually a (free) package that do what you want - Composite.Community.TellAFriend - you install it via "System | Packages | Available Packages | Composite.Community"

You can read about it here: http://docs.composite.net/Composite.Community.TellAFriend