[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Help me convert this Javascript function to C#

Posted on 2008-02-02
4
Medium Priority
?
1,609 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:bemara57
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:jarw
ID: 20808383
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
 

Author Comment

by:bemara57
ID: 20833425
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
 
LVL 3

Accepted Solution

by:
jarw earned 2000 total points
ID: 20834169
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
 

Author Comment

by:bemara57
ID: 20843106
Thx jarw.. works perfect!!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question