Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

property type to set the name for external link

Vote:
 

I have a property with 'Url' type

public virtual Url UrlLink {get; set;}

When I try to set the link to the internal pages, it's picking up the page name as url. (for ex, if I link to home page , it is displaying the url as Home) . 

For external links, it is displaying the complete name (for www.google.com, it is displayed as www.google.com), but how do I set the name for the external url. I do not want complete url to be seen on the front end, rather should have the name displayed ( like google, it should link to www.google.com)

what is the property type I should use to set the name for the external link?

#149269
May 27, 2016 12:45
Vote:
 

Url property has no such functionality out of the box unfortunately. Either extend it yourself = difficult because dojo is not a funny framework.

or use

LinkItemCollection as property instead. This has support both for text, title and targets on all links and supports multiple links. 

or 

add an extra property for text and control the rendering (dont use PropertyFor) and build your own a tag. 

or 

create a local block that does this and control rendering there to build your own a tag. 

I would go with LinkItemCollection normally :)

#149271
May 27, 2016 13:32
Vote:
 

I just want to link to one internal page/external link,
1) Is LinkItemCollection the right choice for it?
2) How do I render it on the view? when I try to use as below, it is still rendering as normal url without any name to set for the link.

@Html.DisplayLinkCollection("<ul>{0}</ul>", "<li>{0}</li>", Model.Url)

#149283
May 27, 2016 15:38
Vote:
 

@Html.PropertyFor()

For a single link it might not be the best match. But...a single item is a list of one link so :)

Otherwise go with local block that has both url and a string property for name. Takes a bit more code though...

#149288
May 27, 2016 16:06
Vote:
 

I have created two properties


public virtual Url ButtonLink { get; set; }
public virtual string ButtonLinkName { get; set; }

In the view, I have rendered as
@Html.DisplayLink(Model.ButtonLink, Model.ButtonLinkName, true)

where DisplayLink is:

public static IHtmlString DisplayLink(this HtmlHelper html, Url url, string text = null, bool allowNoText = true)
{
return html.DisplayLink(url.Resolve(), text, allowNoText);
}

and Resolve function is:

public static string Resolve(this EPiServer.Url url)
{
return url.IsNullOrEmpty() ? null : ServiceLocator.Current.GetInstance<UrlHelper>().ContentUrl(url);
}

With the above code,
1) when the external link is entered and no name is given, it is displaying the complete url

2) When the external link is entered and name is given, it is displaying the Name as url and is redirecting to
the url given

3) when internal link is selected and name is given, it is displaying the name and is redirecting to the right page when clicked on it

4) When internal link is selected and no name is given, it is displaying the friendly url (
when home page is selected as the url, in the front end it's displaying as '/', if it's some other page it is displaying as '/page1/page2').
When clicked on friendly url, it is redirecting to the right page.

In the scenario 4, I want the page name to be displayed rather than friendly url. In the example above, Home needs to be displayed as opposed to '/',
in place of '/page1/page2', page2 should be displayed. And it should redirect to the right page when clicked on it. Scenarios 1 to 3 should not
get affected.

#149301
May 28, 2016 11:17
Vote:
 

1. Use UrlResolver to get the content from url. Use the Route method on that class...

2. Get the page name from the content item.

3. If ButtonLinkName above is empty, use the above...

#149304
May 28, 2016 23:57
* 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.