You can try using EPiBoost (PageDataExtensions), theres a load of extensions in there which will make your life a little easier.
@JMenziesSmith: I've tried using the extensionmethods GetExternalUrl and GetHref on the PageData object but both return the same url starting with "/episerversecure/cms/" so unfortunately that doesn't seem to help, or should I use another extension method?
@Joel: Unfortunately we're not using MVC (I'm upgrading an old site) so we're working with web forms and I have installed patch 1, but it still doesn't work.
Any other suggestions? Otherwise I will have to try to manually remove the edit-mode prefix from the URL but that doesn't feel like a particulary good solution. Or is there some way to pin point the correct RouteCollection for the site, since it seems to be one collection for edit-mode and another one for the actual site, if I understand it correctly?
/Martin
Have you tried the overload to GetVirtualPath that takes in RequestContext as parameter? You could try to use that and explicitly set context mode to defualt before calliing GetVirtualPath. Something like:
var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var requestContext = new RequestContext()
{
RouteData = new RouteData()
};
requestContext.RouteData.DataTokens["contextmode"] = ContextMode.Default;
var url = urlResolver.GetVirtualPath(new ContentReference(8), ContentLanguage.PreferredCulture.Name,
null, requestContext).GetUrl();
@Johan: thanks for your reply but unfortunately that gives me the same result as before. Here's my code:
var resolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var requestContext = new RequestContext()
{
RouteData = new RouteData()
};
requestContext.RouteData.DataTokens["contextmode"] = ContextMode.Default;
var url = resolver.GetVirtualPath(new ContentReference(pageData.ContentLink.ID), ContentLanguage.PreferredCulture.Name, null, requestContext).GetUrl();
Can you see something that I might have missed?
When I try this locally (I am running latest on our Development branch) then it works so obviously something has changed. That will of course not help you...:-(
I will look in the maintenace branch and see if I can figure out a way to make it work. I'll get back
Thank you Johan! I'll also have a look at a Alloy site to make sure that it isn't anything else in our code that causes this behaviour somehow.
Ok, now I have investigated further and found that there is an issue in the CMS7 release. There is already a bug registered for it and the bug has been fixed
#91931: Unable to get PublicUrl in edit mode
This will be included in the next patch (that should be available soon).
OK, good to know, then I'll leave this problem for now. By the way, I tried to look into known bugs in EPiServer 7 on http://world.episerver.com/Support/Bug-List/ but can't see EPiServer 7 as an option there. Should I be looking somewhere else for these bugs?
/Martin
Hello Johan,
I just installed Patch 2 for EPiServer 7 and the problem still exists, also I can't find a reference to this bug (#91931) in the list of fixed bugs in Patch 2, do you have any clue about when Patch 3 might be available and if this bug will be solved in that patch?
Best regards
Martin
Is there any update on this issue? I am running patch 4, and it is still returning the wrong url, very frustrating
Hello Erik,
I've received the information, but forgotten to post it here, that this will be fixed in EPiServer 7.5 and I also received a workaround solution for the problem which is as follows:
RequestContext requestContext = new RequestContext();
requestContext.RouteData = new RouteData();
requestContext.RouteData.DataTokens.Add("contextmode",ContextMode.Default);
RouteValueDictionary routeValues = new RouteValueDictionary();
var urlResolver = new UrlResolver();
var contextSaved = HttpContext.Current;
HttpContext.Current = null;
var url = urlResolver.GetVirtualPath(ContentLink, Language, routeValues, requestContext);
HttpContext.Current = contextSaved;
So I'm also waiting for the real fix but this helped us out for now.
/Martin
I use this line of code now to get friendly url to pages without any edit-mode references:
UrlResolver.Current.GetUrl(contentLink, null, new VirtualPathArguments { ContextMode = ContextMode.Default })
I hope this is what you're looking for?
Thanks Martin.
That works, but I had hoped it would be even more built in so they had a extension for URL that they have for ContentUrl so you can use it directly from the razor view.
Thanks for the input, I will create my own method that looks like that.
I ended up with a extension method that looks like this (I added support to include baseUri also, since I needed it)
public static string ContentUrl(this UrlHelper urlHelper, ContentReference contentLink, ContextMode contextMode, bool includeBaseUri) { if (ContentReference.IsNullOrEmpty(contentLink)) return string.Empty; var url = new Uri(SiteDefinition.Current.SiteUrl,UrlResolver.Current.GetUrl(contentLink, null, new VirtualPathArguments {ContextMode = contextMode})); return includeBaseUri ? url.AbsoluteUri : url.PathAndQuery; }
Hello,
First of all I have to say that I'm quite new when it comes to working with EPiServer 7 so if this is a really simple question I do apologize, but hope to get an answer anyway :-)
I'm trying to fetch the friendly url to a page from code behind but when doing so within edit-mode the friendly url that I get is the friendly url that I assume is the one that's being used by edit mode. For instance let's say I have a page that has the a friendly url that looks like this:
/Segment1/Segment2/
When using the method GetVirtualPath in both UrlResolver (ServiceLocator.Current.GetInstance<UrlResolver>()) and in the RouteCollection extension methods provided by EPiServer I get the following "friendly url" in Edit mode ("episerversecure" being the search path to the backend system):
/episerversecure/CMS/Segment1/Segment2,,6596/?id=6596&epieditmode=true
When calling the same functions from outside of edit mode I get the friendly url back ("/Segment1/Segment2").
Any help is appreciated!
Best regards
Martin