Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Valcourt 70 posts 103 karma points
    May 03, 2017 @ 16:45
    Robert Valcourt
    0

    umbracoUrlAlias not working when using locaLink?

    Trying to implement the use of umbracoUrlAlias to my v6.2.6 site. If I add the property to the doctype and specify a value, it works in that the page displays. EG:

    http://www.mysite.com/level1/level2/somepage/ has a umbracoUrlAlias value of 'foobar'. After publishing, the following URL is valid:

    http://www.mysite.com/foobar/

    However, if I link to the page using {localLink:1024} in the templates, the original URL is provided:

    http://www.mysite.com/level1/level2/somepage/

    If I use umbracoUrlName instead, and I use {localLink:1024} in the templates, the following is returned:

    http://www.mysite.com/level1/level2/foobar/

    My goal is to have localLink return the proper URL as defined by umbracoUrlAias or umbracoUrlName. Is this possible?

    Many thanks.

  • Victor 25 posts 146 karma points
    May 03, 2017 @ 17:02
    Victor
    0

    That is working as intended, the alias is just other URL that your content goes by. To reference the alias instead of the original URL, you'd have to change your structure to do it so.

    Normally, you'd create the umbracoUrlAlias on the SEO document type, so all pages would have it. If you do that, you can easily get that property on your content and use it instead.

    Example:

        if(Model.Content.HasValue("umbracoUrlAlias"))
        {
             var Link = HttpContext.Current.Request.Url.Host + "/" + Model.Content.GetProperty("umbracoUrlAlias").Value.ToString();
             <a href="@Link">Link</a>
        }
        else
        {
             <a href="@Model.Content.Url">Link</a>
        }
    
  • Robert Valcourt 70 posts 103 karma points
    May 03, 2017 @ 18:23
    Robert Valcourt
    0

    I created a new codebehind file to accommodate this. FixAliases.cs in App_Code folder. Site uses older webforms templates:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    using TheOutfield.Routing;
    using Umbraco.Core;
    using Umbraco.Web.Routing;
    
    namespace TheOutfield.Routing
    {
    public class UrlAliasProvider : IUrlProvider
    {
        public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
        {
            var node = umbracoContext.ContentCache.GetById(id);
            return node.HasValue("umbracoUrlAlias")
                ? "/" + node.GetPropertyValue<string>("umbracoUrlAlias") + "/"
                : null;
        }
    
        public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
        {
            return Enumerable.Empty<string>();
        }
      }
    }
    
    namespace TheOutfield
    {
    public class Bootstrapper : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, UrlAliasProvider>();
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        { }
      }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft