Copied to clipboard

Flag this post as spam?

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


  • Patric Martin 4 posts 44 karma points
    Apr 18, 2016 @ 14:56
    Patric Martin
    0

    Override IpublishedContent.Url

    Hi!

    I'm building a website where most of the pages can be modified by querystring parameter. For example if a parameter "member" is passed in the header renders a different logo. From that moment on all links should use it for further use. As I generally use the IPublishedContent.Url property to link to other pages it is probably the best solution to modify the URL property by just adding ?member=xxx to the URL globally. Is there any way to achieve this?

  • Sven Geusens 169 posts 881 karma points c-trib
    Apr 20, 2016 @ 12:35
    Sven Geusens
    0

    Create 2 Extension classes, 1 for Umbracohelper (if you use it) and 1 for IPublishedContent and 1 helper class for the shared logic.

    public static string UrlWithCopiedParams(this UmbracoHelper umbracoHelper, IPublishedContent content, HttpRequestBase request)
        {
            return UrlParams(content, request);
        }
        public static string UrlWithCopiedParams(this IPublishedContent content, HttpRequestBase request)
        {
            return UrlParams(content, request);
        }
    
        private static string UrlParams(IPublishedContent content, HttpRequestBase request)
        {
            string[] allowedparameters = { "paramName1", "paramName2" };
            string parameters = "";
    
            if (allowedparameters.Count() > 0)
            {
                foreach (string param in allowedparameters)
                {
                    if (!string.IsNullOrEmpty(request[param]))
                    {
                        if (parameters.Length == 0)
                        {
                            parameters = string.Format("?{1}={2}", parameters, param, request[param]);
                        }
                        else if (parameters.Length > 1)
                        {
                            parameters = string.Format("{0}&{1}={2}", parameters, param, request[param]);
                        }
                    }
                }
                return content.Url + parameters;
            }
            else
            {
                return content.Url;
            }
        }
    

    And then use one of these 2 calls in your views(you will most likely use the second one)

    @Umbraco.UrlWithCopiedParams(Model.Content,Request)
    @IPublishedContent.UrlWithCopiedParams(Request)
    

    If anyone can give me a workaround on how to get the Request without passing it, I would be gratefull.

  • Steven Harland 78 posts 518 karma points c-trib
    Apr 20, 2016 @ 16:20
    Steven Harland
    1

    Hi Sven,

    See my example. The idea is pretty much the same. You can get the current request using HttpContext.Current.Request.

  • Simon steed 376 posts 688 karma points
    Apr 20, 2016 @ 12:53
  • Steven Harland 78 posts 518 karma points c-trib
    Apr 20, 2016 @ 13:07
    Steven Harland
    100

    Hi Patric,

    You could achieve this with an extension method on IPublishedContent:

    public static class PublishedContentExtensions
    {
        public static string MyUrl(this IPublishedContent content)
        {
            var member = HttpContext.Current.Request.QueryString["member"];
    
            if (string.IsNullOrEmpty(member))
            {
                return content.Url;
            }
    
            return content.Url + "?member=" + member;
        }
    }

    Put this class somewhere in your project and then in your views you'll be able to do something like this:

    <a href="@Model.Content.MyUrl()">@Model.Content.Name</a>
    
  • Patric Martin 4 posts 44 karma points
    Apr 22, 2016 @ 11:56
    Patric Martin
    0

    Thanks a lot, that was exactly what I was looking for!

Please Sign in or register to post replies

Write your reply to:

Draft