3

I have a simple task without a simple solution. I have a parameter in the browser that needs to be changed or rewritten

for instance www.contoso.com/countries.aspx?country=UK

all I need is to rewrite the parameter without checking the url so it might appear as:

www.contoso.com/countries.aspx?country=France

I have tried something like that but with no joy

string parameter2 = Request.QueryString["country"];
Context.RewritePath(parameter2.Replace("?country=", "France"));
walther
  • 13,466
  • 5
  • 41
  • 67
user1211929
  • 1,190
  • 1
  • 12
  • 32

4 Answers4

1

You could do something like this:

var url = "www.contoso.com/countries.aspx?country={0}";

var country = "UK";

url = String.Format(url, country);

Alternatively you can do:

var url = Request.Url.AbsolutePath;

var country = Request.QueryString["country"];

url = url.Replace(country, "UK");

Then:

Response.Redirect(url);

Carl Winder
  • 938
  • 8
  • 18
  • 1
    Thanks, the url already comes as www.contoso.com/countries.aspx?country=UK, I need to repost the parameter. – user1211929 Jun 13 '12 at 10:31
  • ahhh so this will only work if you know the URL in advance which I am guessing you do not know it in advance – Carl Winder Jun 13 '12 at 10:34
  • 1
    It worked really well, I tweaked your example a bit, the response.redirect made me understand how to repostback the string thanks – user1211929 Jun 13 '12 at 11:22
0

Can you not read the whole URL into a string, split it on the '?' and then add your new bit to the first part of the string?

Something like this:

    var url = Request.QueryString;
    var newUrl = url.split('?');
    url = newUrl[0] + "?country=France";

I dont know if that will work, its just a thought

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
  • Unnecessary. Why to do it like this? He has problems with rewriting, getting a parameter shouldn't be an issue... – walther Jun 13 '12 at 10:27
0

If you want to replace the complete querystring, use

newVal = string.LastIndexOf("?");

and then

URL.Replace(oldVal, newVal);

OR if you have just one parameter in querystring and want to replace only value of it, use

newVal = string.LastIndexOf("=");
URL.Replace(oldVal, newVal);
Estefany Velez
  • 328
  • 4
  • 18
0

Look at this detailed response for solution to your problem.

Community
  • 1
  • 1
Ebad Masood
  • 2,389
  • 28
  • 46