tags:

views:

50

answers:

2

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;

A: 

It sounds like you want to do this:

string redirect = "www.mysite.com?mykey=" + MyValue.ToString();
Response.Redirect(redirect);
jsumners
@jsumners - Yes - I knew it would be simple! Is this the best way to manage the URL in a ASP.NET Ajax application? For instance, I am using updatepanel and scriptmanager. I have two "pager" buttons each linked to two separate click handlers. So I should build the URL I want in the click handlers?
Timbo
I'm not much more experience with ASP.Net development than you are. So I can't really say what the best practice is.
jsumners
A: 

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.

pst