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;
}
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!
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) %>
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 :)
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.
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;
}
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!
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.