Developer forum

Forum » CMS - Standard features » Guidance on using GetFriendlyUrl

Guidance on using GetFriendlyUrl

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

In many of our web sites I see developers use this method: Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl

I also know that you have been recommending not to call this from custom code as the method depends on context that may or may not be available.

This leads me to the question: should you never use this? Or sparingly knowing the caveats? There are a few scenarios where I think calling this method would be helpful:

1. In building client side AJAX URLs. For example:

@ {
  var cartUrl = Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(cartId);
}
...
$.Ajax(@cartUrl, {});


Without the call to GetFriendlyUrl, I would be calling Default.aspx?ID=123 which then server side would redirect to /cart, right? We call stuff like this quite often so having extra redirects on all calls feels like a waste. Hardcoding /cart feels bad too as it means we'll have issues with multi-language sites,

2. Newsletters and other email
For example, when sending a user an email from the Data Integration module we'd like to link to the password reset page. Linking to Default.aspx?ID=456 looks pretty ugly, and I would prefer /my-account/reset-password instead.

How could I accomplish that without GetFriendlyUrls and without hardcoding the page name?

Thanks in advance for any guidance you can give.

Imar


Replies

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Imar,

That's a very good question.

I have often see also the product links in Related products being rendered "unfriendly".

That might also be a case where you want to load Friendly URL's.

Or when you publish an RSS feed with Item based pages, the URL are sometimes rendered unfriendly.

A good guide of how and when to use it, would be very useful.

Looking forward to the response.

Thanks,

Adrian

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Imar

@1: In generel, do not use it, that is true. For this scenario you could - but it is not needed. When you request /cart in an ajax, I would suppose you changed the mime-type of the page on the page properties tab, and that triggers Dynamicweb to not rewrite the URL. You can also add &redirect=false to the querystring and it will leave it alone.

So this one case where it would be ok to call GetFriendlyUrl directly. In this case you also call the GetFriendlyUrl that takes a page id. That one is not context dependent as it simply looks up the URL of the page based on the ID.

@2: This is an example where it might not work. GetFriendlyUrl looks at the domain and user context to find the right URL and in the case of sending and email the context might be wrong. But again, passing in the ID of the page, it would not be an issue.

BR Nicolai

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Oh, I see. So calling SearchEngineFriendlyURLs.GetFriendlyUrl(pageId)  would never be an issue as a unique pageId resolves to a unique URL? That's helpful, as that's what I am seeing most.

Thanks,

Imar

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Guys,

I have a new old use case.

We have a site where we have separate content pages for different product types. Because of this split, the link to the product detail in the search results cannot be rendered correctly.

I have noticed an interesting tag that is unfortunately rendered empty: Ecom:Product.Canonical. I assume it should be render the canonical URl tpo the product which might be very helpful in this case.

I have set PrimaryGroup and also primary page for the group but the tag is still empty.

This would be a good case for generating a friendly URL for the combination ID, GroupID,ProductID

Any other solution?

Thanks,

Adrian

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

But doesn't the system do that automatically when you link to ?ID=123&GroupID=GROUP1&ProductID=PRODUCT1?

 

 
Nicolai Pedersen
Reply

Yes Imar... Exactly.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Sometimes yes.

See here: http://www.austral.ro/catalog/arhivare/bibliorafturi/biblioraft-a4-pvc-50-mm-ubers

"Produse recomandate" is listed with the regular Recommended Products loop, the link tag is Product.Linkgroup.Clean (although I tried every possible tag) and the link is "Default.aspx?ID=3250&GroupID=GROUP424&ProductID=VPR004"

Adrian

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

It's because your links are in single quotes

<a href='Default.aspx?ID=3250&GroupID=GROUP424&ProductID=VPR004' alt='Videoproiector BenQ SH940' title='Videoproiector BenQ SH940'>

Change them to double quotes and it should work:

<a href="Default.aspx?ID=3250&GroupID=GROUP424&ProductID=VPR004" alt='Videoproiector BenQ SH940' title='Videoproiector BenQ SH940'>

I don't think the RegEx that detects these links takes single quotes into account.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Thank you Imar. That did it.

I never thought about that neither I have seen any mention of this behavior.

Probably I need to read more :)

Thank you,

Adrian

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I learned it the hard way: http://doc.dynamicweb.com/forum/cms-standard-features/output-replacement-for-friendly-urls?M=BasicForum&PID=1605 ;-)

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

>> When you request /cart in an ajax, I would suppose you changed the mime-type of the page on the page properties tab, and that triggers Dynamicweb to not rewrite the URL. You can also add &redirect=false to the querystring and it will leave it alone.

I just tried this as follows:

miniCart: '@GetString("Item.Area.MiniCart")&redirect=false',

which results in:

Default.aspx?ID=104&redirect=false

Yet when requested, it redirects to /mini-cart?redirect=false

How do I prevent the redirect? Note, this is on DW 9.2.8.

Thanks!

 
Nicolai Pedersen
Reply

Hi Imar

Seems like the redirect=false does not exist for pageview - so if you explicitly set a mimetype, it is not redirected. You can set the mime type to text/html if that is what you require...

BR Nicolai

 

You must be logged in to post in the forum