Easily create a querystring on the fly?

D

David

Hello

I need to take some values and place them in a string in
the format of a querystring.

Lets say I have:

string State = "ACT";
string SearchString = "Tax";
int MinRelevance = 50;

I want to create the string:

"?State=ACT&SearchString=Tax&MinRelevance=50"

This will then be placed on the end of the relevant URL,
and placed in an anchor tag on the resulting page, so the
user can click on that link.

Now in an ideal world, I would be able to do:

System.Web.QueryString x = new System.Web.QueryString();
x.Add("State", State);
x.Add("SearchString", SearchString);
x.Add("MinRelevance", MinRelevance);
return (x.ToString());

At the moment, I am looking at writing my own function to
do this. It will take a namevaluecollection, and iterate
through those, creating the querystring. Then I would
have to use System.Web.HttpUtility.UrlEncode. I would
assume I will have to add on a dummy URL and remove it
after the UrlEncode as well.

Anyway, if there is an object function somewhere that
already does this, I would love to hear about it.

Thanks
David
 
F

Frank Oquendo

David said:
"?State=ACT&SearchString=Tax&MinRelevance=50"

There's always string.Format. Yould also use the Cache, Application and
Session objects to avoid passing that information so visibly.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
D

David

No, I actually want that data in the querystring. People
should be able to right click on the link and bookmark
them. The info is entered by the user, so does not need
to be secure.

I cannot see how string.format will help me. I really
want to use the Querystring Object without it being
linked to an actual web page. It is so easy wo add new
values to the querystring of an actual page, but nothing
to help you if you want to build querystrings for 10
links on a page.

David
 
J

Jacob Yang [MSFT]

Hi David,

I am not sure where you want to append the querystring to a URL. As I
understood, if you want to change the action property of this webform by
appending the querystring, you can override the Render method, where you
can replace the name of current web form with the same one followed by the
querystring.

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

David

Thats not what I wanted.

I have created an object to do the trick. Code is posted
here for anyone else who wants this functionality.

Jacob, if there is an object in the framework that
already does this, let me know. And the
Page.Request.QueryString is no good, as I am not adding
this querystring to the current page. I am making up 10
different links to place in anchors on the web page. I
must make a querystring for each of these 10 links.

==================================
using System;
using System.Collections.Specialized;

namespace ITR.Web.HttpUtility
{
/// <summary>
/// Summary description for QueryString.
/// </summary>
public class QueryString : NameValueCollection
{

private string _queryStringStartChar
= "?";
private string _queryStringSeparatorChar
= "&";

/// <summary>
/// Returns a UrlEncoded Querystring
representation of the values in the namevaluecollection
/// </summary>
/// <returns></returns>
public override string ToString()
{
System.Text.StringBuilder
ReturnValue = new System.Text.StringBuilder();
bool FirstValue = true;

// Only fill the Stringbuilder if
there are values in the NameValueCollection
if (this.Count > 0)
{
// Add the questionmark
ReturnValue.Append
(_queryStringStartChar);

// Add all the values in
the NameValueCollection
foreach (string Key in
this.AllKeys)
{
// Add an
ampersand if this is not the first value
if (!FirstValue)

ReturnValue.Append(_queryStringSeparatorChar);

// Add the next
value
ReturnValue.Append
(System.Web.HttpUtility.UrlEncode(Key) + "=" +
System.Web.HttpUtility.UrlEncode(this[Key].ToString()));
}
}

return (ReturnValue.ToString());
}

} // QueryString

} // ITR.Web.HttpUtility
====================================

David
 
J

Jacob Yang [MSFT]

Hi David,

Thank you for sharing your wonderful solution.

I have done more research regarding this issue. Unfortunately, there is not
such a .NET Framework class that can do the same functionality. Thank you
for your understanding.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top