Removing sitecore content from url

16. oktober 2009 by Thomas Stern

A small a simple methode for removing "sitecore/content/from url"

first off we need to overwrite the linkprovider so links made from the linkmanager doesn't contain "sitecore/content.

 

public class LinkProvider : Sitecore.Links.LinkProvider
  {
    /// 
    /// Gets the (friendly) URL of an item.
    /// 
    /// The item.
    /// The options.
    /// 
    /// Created 15-10-2009 11:57 by ts
    public override string GetItemUrl(Sitecore.Data.Items.Item item, Sitecore.Links.UrlOptions options)
    {
      //Remove sitecore/content from url
      //Override of itemresolver necessary to handle this 
      //Dismiss the sitecore security domain
     if (Context.Site.Name.Equals(Sitecore.Constants.ShellSiteName, 
         StringComparison.InvariantCultureIgnoreCase) ||
         Sitecore.Context.PageMode.IsDebugging ||
         Sitecore.Context.PageMode.IsPageEditor || 
         Sitecore.Context.PageMode.IsPreview)

        return base.GetItemUrl(item, options).Replace("sitecore/content/","").Replace(".aspx","");
      return base.GetItemUrl(item, options);
    }
  }


So now links is rendered without "sitecore/content/" prefix. Now we need to override the itemresolver so the we still have access the correct items. We first need to see if the item where placed under the localsite or in some global folder outside the website

 

 

class ItemResolver : Sitecore.Pipelines.HttpRequest.ItemResolver
  {

    /// 
    /// Runs the processor.
    /// 
    /// The arguments.
    /// Created 15-10-2009 13:56 by ts
    public override void Process(HttpRequestArgs args)
    {
      base.Process(args);
      if (Context.Item == null)
        Context.Item = ResolveFromShortPath(args);

    }

    /// 
    /// Resolves from short path.
    /// 
    /// The args.
    /// 
    /// Created 15-10-2009 13:56 by ts
    private Item ResolveFromShortPath(HttpRequestArgs args)
    {

      //Is item placed under the site
      string localPath = String.Format("{0}", MainUtil.DecodeName(args.Url.ItemPath));
      Item item = args.GetItem(localPath);
      if (item != null)
        return item;
      //Is item placed under a more global folder use the localpath to find it
      string globalPath = String.Format("/sitecore/content{0}", MainUtil.DecodeName(args.LocalPath));
      item = args.GetItem(globalPath);
      return item;

    }
  }


So one could argue that these to checks incresses performence. I haven't done test on that yet. But i'm looking into that

0 comment(s) for “Removing sitecore content from url”

    Leave comment: