1

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

3
  • how are you hitting this Url - ajax call or a direct call to the website? it looks like the value is getting encoded. Also make sure that there is no double quotes. Commented Jan 31, 2015 at 5:43
  • directly using response.redirect(url); Commented Jan 31, 2015 at 5:47
  • you should encode query string values similar to targetURL = "~/?Xresult=" + HttpUtility.UrlEncode(res); and then call response.redirect Commented Jan 31, 2015 at 5:59

2 Answers 2

3

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=");
Sign up to request clarification or add additional context in comments.

2 Comments

you mean to encode query string in the url and then do response.redirect
@sandeep Use Uri.EscapeDataString("your string which may contain the special characters")
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.