Absolute external URL property

Vote:
 

Hi all,

I am looking to implement a twitter button on some of my pages. However if a URL is not defined, it uses the canonical link in the head. I want it to use the current page URL, rather than the canonical url, so need a property that I can place in the twitter code, which returns the absolute URL for external visitors. So far I have only been able to produce the relative URL using CurrentPage.LinkURL.

I am aware of EPiServer.Web.UrlRewriteProvider.UrlPreventRewriteAttribute %>="true" but the twitter code will only allow 1 propery to be used in the url=""

Any help would be appreciated.

#60975
Sep 03, 2012 14:25
Vote:
 

Hi,


Here is an extension method you could use to get the external url to a page:

 

public static string ExternalURL(this PageData p)
{
    UrlBuilder pageURLBuilder = new UrlBuilder(p.LinkURL);

    Global.UrlRewriteProvider.ConvertToExternal(pageURLBuilder, p.PageLink, UTF8Encoding.UTF8);

    string pageURL = pageURLBuilder.ToString();

    UriBuilder uriBuilder = new UriBuilder(EPiServer.Configuration.Settings.Instance.SiteUrl);

    uriBuilder.Path = pageURL;

    return uriBuilder.Uri.AbsoluteUri;
}

    

#60976
Sep 03, 2012 14:57
Vote:
 

Hi Johan,

Thank you for your reponse. would I place this in the code behind file of the page that I was using the property on, or somewhere else? Would I reference the property <%= CurrentPage.ExternalURL%>? Is there anything else I need to do to call the property?

Thank you for your help!

#60978
Sep 03, 2012 15:40
Vote:
 

Hi again,

You can read about extension methods here http://msdn.microsoft.com/en-us/library/bb383977.aspx.

But you can also just put the method code-behind, but then you have to remove the "this"-keyword in the method and pass CurrentPage to the method like:

 

<%= ExternalURL(CurrentPage) %>

    

#60980
Sep 03, 2012 16:12
Vote:
 

It is an extension method so you call it using CurrentPage.ExternalURL() but for that to work in code-front you also have to import the namespace of the class containing the Extension method. E.g. if you define the method in the class MyProject.MyNamespace.ExtensionMethods (must be a static class) you need to add <%@ Import Namespace="MyProject.MyNamespace" %> to your page/control.

Edit: Ah, too slow :)

#60981
Edited, Sep 03, 2012 16:13
Vote:
 

Hi Again Johan/ Magnus,

I have tried what you have suggested but recieved errors.

The error that I currently have is: The type or namespace name '' does not exist in the namespace ''

When using <%= ExternalURL(CurrentPage)%> I get the name'' does not exist in the current context.

#60985
Edited, Sep 03, 2012 16:57
Vote:
 

So did you put the method in the code-behind? If so, the method should look like:

protected string ExternalURL(PageData p)
{
    UrlBuilder pageURLBuilder = new UrlBuilder(p.LinkURL);

    Global.UrlRewriteProvider.ConvertToExternal(pageURLBuilder, p.PageLink, UTF8Encoding.UTF8);

    string pageURL = pageURLBuilder.ToString();

    UriBuilder uriBuilder = new UriBuilder(EPiServer.Configuration.Settings.Instance.SiteUrl);

    uriBuilder.Path = pageURL;

    return uriBuilder.Uri.AbsoluteUri;
}

    

 

#60987
Sep 03, 2012 17:10
Vote:
 

Hi Johan,

I had another look at this this morning and have been able to get it to work on the page I needed it for, using a public partial class.

Thank you for your help on this!

#60994
Sep 04, 2012 9:59
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.