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"));
2
  • 1
    WHEN are you trying to do this? Commented Jun 13, 2012 at 10:27
  • 1
    after an event like a button or hyperlink, the code should just change that parameter without changing anything else Commented Jun 13, 2012 at 10:32

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, the url already comes as www.contoso.com/countries.aspx?country=UK, I need to repost the parameter.
ahhh so this will only work if you know the URL in advance which I am guessing you do not know it in advance
It worked really well, I tweaked your example a bit, the response.redirect made me understand how to repostback the string thanks
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

1 Comment

Unnecessary. Why to do it like this? He has problems with rewriting, getting a parameter shouldn't be an issue...
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);

Comments

0

Look at this detailed response for solution to your problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.