If you or your client needs to be able to create short urls or links to other sites without wanting to add it as a redirect in the web config, then this is the post for you. You can create them as umbraco pages that act as fast links or redirects.

Firstly, you need to create a template for the document type to use.
I normally create on called 'FastLink'.

Here is the code I use for the template:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
string linkUrl = "";
if(CurrentPage.HasValue("linkTarget"))
{
var linkItem = CurrentPage.linkTarget.First;
linkUrl = linkItem.link;
}

if(!String.IsNullOrEmpty(linkUrl))
{
Response.Redirect(linkUrl);
}
}

Now you need to create a document type, in this case I call it 'Fast Link'. Choose the Fast Link template you just created.

I would then normally create a tab called 'Information'.  Once you have created the tab, you can then create a Related Links property called 'Link Target'. The alias for this property should automatically be set to 'linkTarget'

Make sure you choose the Information tab for this property to appear in.  Once this document type is saved, you are ready to use it in your site.

To be able to add it into the structure of your site, you need to edit the document type that you would like to allow it under, i.e. your 'Home' document type. Go to the structure tab and in the allowed child nodes check box list, choose 'Fast Link'. Then hit save.

You can now create a Fast Link in the site under the home node, or whichever you set it to. When you create it, you will need to add a row to the Link Target field, by entering a caption, choosing an internal page or inputting an external url and then tick whether or not you want it to open in a new tab. (This will come in handy when using it in a menu).
Save the row and Save and Publish the document.

Next visit the url of the item you created e.g. http://www.mysite.com/test/ and it should redirect you to the page or external address you chose.

To finish this off you can use these fast links in your site menu.

Here is an example Partial which generates a site menu from the pages in your site with a simple ul > li structure:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{
var homePage = CurrentPage.AncestorsOrSelf(1).First();
}

<div id="navigation">
<ul>
<li><a href="@homePage.Url">@homePage.Name</a></li>
@RenderChildren(homePage)
</ul>
</div>

@helper RenderChildren(dynamic parentPage)
{
var children = parentPage.Children.Where("Visible").Where("excludeFromTopNavigation != true && documentTypeAlias != \"Xmlsitemap\"");
if(children != null && children.Any() && children.Count() > 0)
{
@:<ul>
foreach(var page in children)
{
string linkUrl = page.Url;
string linkTarget = null;
string linkCaption = page.Name;
<li>
@if(page.documentTypeAlias == "FastLink" && page.HasValue("linkTarget"))
{
var linkItem = page.linkTarget.First;
linkUrl = (bool)linkItem.isInternal ? Umbraco.NiceUrl(linkItem.Value<int>("internal")) : linkItem.link;
linkTarget = (bool)linkItem.newWindow ? "_blank" : null;
linkCaption = linkItem.caption;
}
<a href="@linkUrl" target="@(!String.IsNullOrEmpty(linkTarget) ? linkTarget : null)">@linkCaption</a>
</li>
@RenderChildren(page)
}
@:</ul>
}
}

Notice that when it identifies the document as a Fast Link, it gets the caption, target and url from the first row in the link target field. This means that when the fast link is rendered in a menu, the page can open in a new tab if the new window value was set to true.

If you would like to know more about Umbraco, why not use your 10 day free trial with pluralsight and go through the umbraco courses.

Paul Seal

A .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Web: codeshare.co.uk

Comments