Asp.Net Query String

I use Querystring to pass values โ€‹โ€‹from one page to another. I am trying to implement encoding and decoding using Server.UrlDecode and urlEncode.

The query string returns a null value, but I can verify that the values โ€‹โ€‹were sent to the URL.

Two pages:

QueryString.aspx

protected void Page_Load(object sender, EventArgs e)
{


}
protected void Button1_Click(object sender, EventArgs e)
{
    string id = "1";
    string name = "aaaa";

    string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));


    Response.Redirect(url);
}

;;

On another page:

QueryStringValuesTransfer.aspx:

 public partial class QueryStringValuesTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id1 = Server.UrlDecode(Request.QueryString["id"]);
        string name1 = Server.UrlDecode(Request.QueryString["name"]);
        Response.Write(id1 + name1);

    }

}


I get null values โ€‹โ€‹in id1 and name1.

Any help please.

+3
source share
6 answers

Change this line:

string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
+5
source

Now you only set the values โ€‹โ€‹in the query line, you need to give them names so that you can capture them again:

string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
+3

, -

MyPage.aspx?id=xxx&name=yyy

, ...

+1
string url = string.Format("QueryStringValuesTransfer.aspx?{0}&{1}", Server.UrlEncode(id), Server.UrlEncode(name));

:

string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));
+1

. :

string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name)); 
+1

When creating the URL on the first page, you should do the following:

string url = string.Format("QueryStringValuesTransfer.aspx?id={0}&name={1}", Server.UrlEncode(id), Server.UrlEncode(name));

The query string consists of key-value pairs, you must provide the keys.

+1
source

Source: https://habr.com/ru/post/1766198/


All Articles