Help me convert this Javascript function to C#

I have a Javascript function that works pretty well. It adds any parameters to the URL I specify. If the URL has no parameters, it puts the '?' on the URL and adds the parameter. If there are parameters, then it adds '&' and adds the parameter to the URL. The tricky part is that if the parameter already exists in the URL, then the parameter value is replaced by the one fed into the function. Here's the Javascript function:

function AddUrlParam(url, param)
{        
        if (url.indexOf("?") != -1)
        {
                var d = param.split("=");
                if(url.indexOf(d[0]+"=") != -1)
                {
                        var re = new RegExp("("+d[0]+"=([^=]*))","i");
                        url = url.replace(re,param );
                }
                else
                {
                        url += "&" + param;
                }
        }
      else
            url += "?" + param

      return url;
}

So for example:
AddUrlParam("http://mydomain.com", "test=zzz") returns http://mydomain.com?test=zzz
AddUrlParam("http://mydomain.com?abc=123", "test=zzz") returns http://mydomain.com?abc=123&test=zzz
AddUrlParam("http://mydomain.com?abc=123&test=bbb", "test=zzz") returns http://mydomain.com?abc=123&test=zzz

Can someone help convert this function to C#? Not sure if there maybe something built into C# but would like to use it server side. I would greatly appreciate it.
bemara57Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jarwCommented:
I can't find an easier solution in the Framework (maybe I'm just missing it). The code below should be a valid replacement.  ~ JW
public static string AddUrlParam(string url, string param)
{
   // Get the name of the parameter.
   string name = param.Split('=')[0];
   
   // Get the path and query parts of the URL.
   string[] urlParts = url.Split(new char[] {'?'}, 2);
   string path = urlParts[0];
   string query = (urlParts.Length == 2) ? urlParts[1] : "";
   
   // Add/replace the name/value pair for the parameter.
   if(query.Contains(name + "="))
   {
      Regex re = new Regex(name + "=[^=]*");
      query = re.Replace(query, param);
   }
   else
   {
      query += (String.IsNullOrEmpty(query)) ? "" : "&";
      query += param;
   }
 
   return path + "?" + query;
}

Open in new window

0
bemara57Author Commented:
Thanks but I've been testing various scenarios and everything works great except one scenario. I can't figure out the bug. When I enter this:

        string url = "http://www.somesite.com/Default.aspx?sticky=0&test=111&test2=222";
        Response.Write(AddUrlParam(url, "sticky=1"));

I get this:
http://www.somesite.com/Default.aspx?sticky=1=111&test2=222

Can you see why it's doing this? I know it's probably the RegEx, but I'm not too keen when it comes to that.
0
jarwCommented:
Sorry about that. I had the wrong character in the regular expression. Please change the line with the regular expression to this:

         Regex re = new Regex(name + "=[^&]*");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bemara57Author Commented:
Thx jarw.. works perfect!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.