简体   繁体   中英

Working with URL query Strings in ASP.NET

I am new to working with ASP.NET and want to find the best way to work with URL query strings.

I can get the current value of a URL query string by using Request.QueryString["UrlKey"] , but can I actually modify the URL with code, without doing a form GET submission?

If the user is landing on the page for the first time, what is the easiest way to programmatically create the ?UrlKey=value through the Page_load method? Or am I better of doing this with Javascript or building a redirect Like: string redirect = "www.mysite.com?" + MyKey + "=" + MyValue; string redirect = "www.mysite.com?" + MyKey + "=" + MyValue;

It sounds like you want to do this:

string redirect = "www.mysite.com?mykey=" + MyValue.ToString();
Response.Redirect(redirect);

Sadly there is no standard way to go from a key-value pair to a query string. However, you can do it yourself rather trivially, especially if you use LINQ.

Either way, please use HttpUtility.UrlPathEncode on both the key and value to avoid generating corrupt URLs. If you are doing it in JavaScript, see encodeURIComponent .

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