Try our conversational search powered by Generative AI!

How to get friendly url to page within edit mode?

Vote:
 

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

#66580
Mar 05, 2013 16:56
Vote:
 

You can try using EPiBoost (PageDataExtensions), theres a load of extensions in there which will make your life a little easier.

 

#66586
Mar 05, 2013 19:04
Vote:
 

Hi Martin,

If you're using MVC it cannot be done due to a bug. It's possible that it has been fixed in patch 1 so you could start by applying it unless you already have.

#66587
Mar 05, 2013 20:11
Vote:
 

@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

#66658
Mar 06, 2013 11:14
Vote:
 

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();

#66670
Mar 06, 2013 14:34
Vote:
 

@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?

#66674
Mar 06, 2013 15:26
Vote:
 

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

#66675
Mar 06, 2013 15:36
Vote:
 

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.

#66676
Mar 06, 2013 15:40
Vote:
 

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).

#66677
Mar 06, 2013 15:56
Vote:
 

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

#66678
Mar 06, 2013 15:59
Vote:
 

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

#69151
Mar 22, 2013 16:48
Vote:
 

Is there any update on this issue? I am running patch 4, and it is still returning the wrong url, very frustrating

#77026
Nov 06, 2013 17:39
Vote:
 

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

    

#77028
Edited, Nov 06, 2013 17:46
Vote:
 

thanks for the info, works perfectly!

#77230
Nov 13, 2013 12:43
Vote:
 

Are there any built in solutions for this now?

#132045
Aug 13, 2015 8:12
Vote:
 

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?

#132587
Aug 13, 2015 11:34
Vote:
 

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.

#132589
Aug 13, 2015 12:31
Vote:
 

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;
        }
#132592
Aug 13, 2015 12:54
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.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.