sandeep
sandeep

Reputation: 77

unable to read querystring in asp.net

This is my url which contains 4 querystrings(desc,url,img,title).

http://localhost:4385/Default?desc=Home%20Page&url=http://localhost:4385/&img=http://localhost:4385/images/ribbon-img.png&title=

I read querystrings like below,

 string title = Request.QueryString["desc"];
 string pageurl = Request.QueryString["url"];
 string alttext = Request.QueryString["title"];
 string imageurl = Request.QueryString["img"];

The output i get is:

title=Home Page&url=http://localhost:4385/&img=http://localhost:4385/images/ribbon-img.png&title="

it takes entire url to first querstring, this is not my expected output.

I expect values to all querystring variables

can anyone please help me

Upvotes: 1

Views: 1799

Answers (2)

codeandcloud
codeandcloud

Reputation: 55210

The problem is that you are not creating the QueryString with proper encoding. .NET framework has HttpUtility.ParseQueryString Method to simplify this problem of encoding. Try this code

//are you sure your URL doesn't have an ".aspx" extension?
var url = " http://localhost:4385/Default.aspx?";
var queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["desc"] = "Home Page";
queryString["url"] = "http://localhost:4385/";
queryString["image"] = "http://localhost:4385/images/ribbon-img.png";
queryString["title"] = "";
Response.Redirect(url + queryString.ToString());

Now the QueryString will look like this.

var urlWithQueryString = " http://localhost:4385/Default.aspx?desc=Home+Page&url=http%3a%2f%2flocalhost%3a4385%2f&image=http%3a%2f%2flocalhost%3a4385%2fimages%2fribbon-img.png&title="

Now parsing can be done using the method you tried

string title = Request.QueryString["desc"];
string pageurl = Request.QueryString["url"];
string alttext = Request.QueryString["title"];
string imageurl = Request.QueryString["image"]; //you have wrongly typed "img" here

Upvotes: 0

MANOJ GOPI
MANOJ GOPI

Reputation: 1279

The URL format is incorrect i feel, because the slash / character will be sent as %2F in the query string but that was not done in your URL format.

Update:

Respose.Redirect("http://localhost:4385/Default?desc=Home%20Page&url="+Uri.EscapeDataString("http://localhost:4385/")+"&img="+Uri.EscapeDataString("http://localhost:4385/images/ribbon-img.png")+"&title=");

Upvotes: 3

Related Questions