简体   繁体   English

为什么我的参数值未在QueryString中传递

[英]why is my parameter value not being passed in QueryString

I have a parameter: 我有一个参数:

string custName = "";

custName = AddressDT.Rows[0]["first_name"].ToString() + " " + AddressDT.Rows[0]["last_name"].ToString();

I am performing my Response.Redirect 我正在执行我的Response.Redirect

Response.Redirect("~/Account/EmailError.aspx?parm1=custName");

and I am retrieving the parameter value: 我正在检索参数值:

custName = Request.QueryString["parm1"];

when I run debug: ... custName = "custName" 当我运行调试时:... custName =“ custName”

what am I doing wrong? 我究竟做错了什么? I have done this before with no issue. 我之前没有做过这个。

Your Response.Redirect is passing the string "custName", not the value of the variable. 您的Response.Redirect正在传递字符串“ custName”,而不是变量的值。

This will fix it for you: 这将为您解决:

Response.Redirect("~/Account/EmailError.aspx?param1=" + custName);

That is because you're using a String Constant as the value for the parameter. 那是因为您使用字符串常量作为参数的值。

Response.Redirect("~/Account/EmailError.aspx?parm1=custName");

That would always cause the URL to be set with the string custName . 这将始终导致使用字符串custName设置URL。

Try using the variable name as: 尝试将变量名用作:

Response.Redirect("~/Account/EmailError.aspx?parm1=" + custName);

Now the variable would be appended to the request. 现在,该变量将附加到请求中。

Now when you'll run the code, it would produce the value in the Variable and you'll get the code running fine. 现在,当您运行代码时,它将在变量中产生值,并使代码正常运行。

Always remember to finish the string and then adding the variable values. 始终记得先完成字符串,然后添加变量值。 Everything inside the double qoutes is considered to be a string and not a variable name or a keyword. double qoutes内部的所有内容都被视为字符串,而不是变量名或关键字。

If you already know that QueryString value is string, you need to use UrlEncode . 如果您已经知道QueryString的值是字符串,则需要使用UrlEncode

In addition, you want to use String.Format as much as possible for good design practice instead of +. 另外,为了更好的设计实践,您希望尽可能使用String.Format而不是+。

string custName = String.Format("{0} {1}", 
   AddressDT.Rows[0]["first_name"].ToString(), 
   AddressDT.Rows[0]["last_name"].ToString());

string url = String.Format("~/Account/EmailError.aspx?parm1={0}", 
   Server.UrlEncode(custName));

Response.Redirect(url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM