读书人

new的用法解决办法

发布时间: 2013-10-03 17:28:15 作者: rapoo

new的用法
本帖最后由 seandro 于 2013-10-01 00:15:47 编辑 Driver的CS的代码

public class Driver
{
public int StrToInt32(string e)
{
if (!string.IsNullOrEmpty(e))
{
try
{
return Convert.ToInt32(e);
}
catch (Exception ex) { }
}
return 0;
}
public string GetUrlStr(string e)
{
if (!string.IsNullOrEmpty(Request.QueryString[e]))
return Request.QueryString[e];
return string.Empty;
}
}


web页面A:
public partial class index : System.Web.UI.Page
{
private Driver Driver = new Driver();

public string GetUrlStr(string e)
{
if (!string.IsNullOrEmpty(Request.QueryString[e]))
return Request.QueryString[e];
return string.Empty;
}

protected void Page_Load(object sender, EventArgs e)
{
userid= Driver.StrToInt32(GetUrlStr("userid"));
}

}


web页面B:
public partial class index : System.Web.UI.Page
{
private Driver Driver = new Driver();

public string GetUrlStr(string e)
{
if (!string.IsNullOrEmpty(Request.QueryString[e]))
return Request.QueryString[e];
return string.Empty;
}

protected void Page_Load(object sender, EventArgs e)
{
userid= Driver.StrToInt32(Driver.GetUrlStr("userid"));
}

}


web页面A和B只是在Driver.GetUrlStr("userid")这里,一个用的GetUrlStr("userid"),一个用的Driver.GetUrlStr("userid")
为什么用Driver.GetUrlStr("userid")的,当userid变,返回的值不变呢,都是第一次请求的值


new?class?c#?.net
[解决办法]
引用:
private static HttpRequest Request = HttpContext.Current.Request;


问题就在static了 static在整个进程内只有一份,因此,你取得的只是第一次的值。

把Request传进函数。或者在函数内部用HttpContext.Current.Request取得。

读书人网 >C#

热点推荐