简体   繁体   中英

c# rewrite url parameter

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

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

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

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

查看详细答复以找到解决问题的方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM