Custom route bug

Vote:
 

I've followed Jon's excellent post here: http://jondjones.com/episerver-segments-explained-registering-custom-routes-in-episerver/

So I have a custom route that initially works as expected - browsing mysite.com/searchterm passes 'searchterm' to my controller, giving me a nice, friendly URL. Using querystring parameters also works (mysite.com/?search=searchterm).

Here's the problem:

If I create a link in the TinyMCE editor to a page that matches the route: (like mysite.com/searchterm), things get weird.

Initially, viewing the HTML shows the link in the friendly format: mysite.com/searchterm. If I check the database at this point, it's stored like this: “~/link/some-guid.aspx?epsremainingpath=searchterm”. Browsing the site after the first publish, the link works as expected.

However, if I edit the page again and view the HTML, the link is now shown in this format: "/episerver/CMS/Content/,,1040/searchterm?epieditmode=False&epsremainingpath=searchterm". After saving the page, the database is now updated with this value. Now anyone browsing the page will also be presented with this link, which does not resolve to the friendly URL. So essentially, subsequent edits to a page break any links on the page that match the route! This is quite a nasty error, because a page author can basically break all links without touching them. It doesn't break if I use the querystring format (mysite.com/?search=searchterm)

I've also just tried following Joels post here, using an Initializable module:http://joelabrahamsson.com/custom-routing-for-episerver-content/. Interestingly, the GetVirtualPathSegment method never gets hit, and ultimately I get the same problem.

Can anyone replicate this issue? Anyone have an idea how to fix it?

Thanks,

#148684
May 20, 2016 6:13
Vote:
 

Hey! I just saw this post: http://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2016/5/internal-links-with-query-parameters/

Could be the same issue: I tried the protocol-relative URL - instead of http://mysite.com/searchterm, id did //mysite.com/searchterm and it seems to have worked.

#148685
May 20, 2016 6:21
Vote:
 

Hi

The reason you get that behaviour is that during the second post from the editor the link looks like "/episerver/CMS/Content/,,1040/searchterm?epieditmode=False&epsremainingpath=searchterm" as you noticed. However for this url your custom route will not match since there is segments /episerver/Cms/Content/ first and also a segment /,,1040/. And hence no route matches the url CMS assumes it is an external link and saves it untouched. So what you need to do is to register an additional route that handles the edit format of the link. Like:

routes.MapContentRoute(
                  name: "searchterm",
                  url: CmsHomePath + "{language}/{nodeedit}/{partial}/{action}/{page}",
                  defaults: new { action = "index" },
                  parameters: parameters);
where CmsHomePath is defined as:
 private static string CmsHomePath
    {
        get
        {
            var cmsContentPath = VirtualPathUtility.AppendTrailingSlash(ModuleResourceResolver.Instance.ResolvePath("CMS", "Content"));
            //Remove application specific path if exist.
            return VirtualPathUtilityEx.ToAppRelative(cmsContentPath).Substring(1);
        }
    }
#148704
May 20, 2016 15:35
Vote:
 

Awesome, thank Johan. Adding that extra route basically fixed it.

The issue discussed here:http://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2016/5/internal-links-with-query-parameters/  is happening, but we can live with that for now. 

Essentailly, I can add a link in the 'External link' text field and it ends up as a page reference in the 'Page' textfield, minus the '/searchterm'. However, the link still works as expected (using the HTML button in TinyMCE shows the full path.) Editing is a bit annoying, but at least the page doesn't break anymore.

Interestingly, using the 'classic' link dialog just shows the full internal path, like "http://mysite.com/episerver/CMS/Content/,,1040/searchterm?epieditmode=False&epsremainingpath=searchterm", which possibly makes editing a bit easier.

Thanks,

#148739
May 23, 2016 4:07
Vote:
 

So, it's been a while, but I've encountered another bug related to this implementation of custom routes.

Johan's fix works for the TinyMCE editor, but I get another error when using the page 'Shortcut' (redirect) feature (All properties > Settings tab > Shortcut).

Let's say we have http://mysite.com/searchterm, where searchterm is the parameter.

If I create another regular page, then set it's 'Shortcut' property to 'http://mysite.com/searchterm', I have to use the 'Shortcut to page on another website' option because of the URL parameter. When I save the page, this URL is changed to ~/link/some GUID.aspx?epsremainingpath=searchterm. So now the Shortcut/redirect is broken.

the only workaround I can use is to enter an external URL using traditional querystring format, like http://mysite.com/?query=searchterm.

Thanks

#183361
Oct 12, 2017 4:53
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.