We help IT Professionals succeed at work.

Is there an ASP.NET way to add parameters to the URL?

bemara57
bemara57 asked
on
Medium Priority
1,903 Views
Last Modified: 2013-11-07
I have an onclick button that redirects the user to another page, but I need to add a parameter to the URL for the redirect. Considering all the scenarios, I have nested if statements to get the URL right like this:

        if (!Request.RawUrl.Contains("sticky=1"))
        {
            if (Request.RawUrl.Contains("?"))
                Response.Redirect(Request.RawUrl + "&sticky=1");
            else
                Response.Redirect(Request.RawUrl + "?sticky=1");
        }
        else
            Response.Redirect(Request.RawUrl);

I was hoping there was something built into .NET to add parameters to the URL, like a one liner or something. Can .NET take care of this or do I got to leave the mess like that? I'm worried there's some other scenario I'm not taking into account.
Comment
Watch Question

President/Principal Consultant
Commented:

Author

Commented:
Thx, I just ended up doing this:

    public static string AddUrlParam(string url, string param)
    {
        if (!url.Contains(param))
        {
            if (url.Contains("?"))
                url += "&" + param;
            else
                url += "?" + param;
        }

        return url;
    }

Explore More ContentExplore courses, solutions, and other research materials related to this topic.