현재페이지의 쿼리문자열 한꺼번에 가져오기
이전 웹페이지에서 GET방식을 이용해 해당페이지로 폼의 쿼리문자열을 보낸경우
이들 쿼리문자열 변수의 컬렉션의 key값과 value값을 모두 출력하는 예제입니다.
현재 요청 페이지의 URL은 아래와 같다고 가정합니다.
http://localhost:90/WebApplication1/WebForm1.aspx?id=ssey&name=Seyoung
private void Page_Load(object sender, System.EventArgs e)
{
// Load NameValueCollection object.
NameValueCollection querycollection = Request.QueryString;
// NameValueCollection 클래스를 사용하기 위해서는
// namespace에 using System.Collections.Specialized; 추가.
// Get names of all keys into a string array.
String[] arrKeys = querycollection.AllKeys;
for (int i = 0; i < arrKeys.Length; i++)
{
Response.Write(string.Format("Key[{0}] : {1} <br> ",
i, Server.HtmlEncode(arrKeys[i])));
// == arrKeys.GetValue(i).ToString()
string[] arrValue = querycollection.GetValues(arrKeys[i]);
for(int j=0; j<arrValue.Length; j++)
Response.Write(string.Format("Value[{0}] : {1} <br> ",
j, arrValue[j].ToString()));
}
}
[결과]
Key[0] : id
Value[0] : ssey
Key[1] : name
Value[0] : Seyoung