繁体   English   中英

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

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

我有一个参数:

string custName = "";

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

我正在执行我的Response.Redirect

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

我正在检索参数值:

custName = Request.QueryString["parm1"];

当我运行调试时:... custName =“ custName”

我究竟做错了什么? 我之前没有做过这个。

您的Response.Redirect正在传递字符串“ custName”,而不是变量的值。

这将为您解决:

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

那是因为您使用字符串常量作为参数的值。

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

这将始终导致使用字符串custName设置URL。

尝试将变量名用作:

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

现在,该变量将附加到请求中。

现在,当您运行代码时,它将在变量中产生值,并使代码正常运行。

始终记得先完成字符串,然后添加变量值。 double qoutes内部的所有内容都被视为字符串,而不是变量名或关键字。

如果您已经知道QueryString的值是字符串,则需要使用UrlEncode

另外,为了更好的设计实践,您希望尽可能使用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