1

I've got a page in my Intranet app where sometimes there's a variable attached and sometimes there isn't. For instance, in my pagination code, I've got this:

MenuItem itemMessage = NavMenu.FindItem("First");
itemMessage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1&sid=6";

and for other sections I have this:

MenuItem itemMessage = NavMenu.FindItem("First");
itemMessage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";

Sometimes sid is passed to the page, and sometimes it's not.

The problem is, on the other end, I'm trying to read the values like this:

var queryStrings = HttpUtility.UrlDecode(Request.QueryString.ToString());
var arrQueryStrings = queryStrings.Split('&');
var part1 = arrQueryStrings[0];
var part2 = arrQueryStrings[1];

If sid isn't passed, I get an error for part2 because it's outside the scope of the array.

Any idea how to handle this?

2
  • Look at the second answer in this post stackoverflow.com/questions/646365/… Commented Feb 28, 2017 at 18:29
  • That guy is just pulling back one variable. I need to pull back two if there are two, and one if there is one. I'm thinking I could check querystrings to see if "&" exists, because that's how I'll know if there are two variables or just one. But I have no idea how to do that in C#. Commented Feb 28, 2017 at 18:32

1 Answer 1

2

Use the query string object to request specific items instead of manually parsing the string.

string part1 = Request.QueryString["page"];
string part2 = Request.QueryString["sid"];

if(part2 != null)
{//check if part2 is sent
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! I had a feeling there was an easier way.
If you're going to convert sid to an integer I would use string.IsNullOrWhiteSpace(part2) instead of just a null check.

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.