在asp.net服务器端和客户端将多少种类型的方法从一个页面传递到另一个页面的方式

时间:2017-07-24 08:07:17

标签: asp.net server-side webmethod

我想将值从一个页面传递到另一个页面。有些人认为我知道I. E. Session,cookies,querystring但我不想使用它。另外我想在静态方法服务器端获取值。但我不知道如何进入静态服务器端方法。

谢谢。

2 个答案:

答案 0 :(得分:2)

k.Soni,

我无法发表评论所以我回答了答案。

  

为什么你不想使用session,cookies和querystring?

我想,你得到错误从静态方法中获取会话,cookie和查询字符串变量的值。所以你不想使用tat变量。这是对的吗?

如果是,那就错了。我们可以在HttpContext.Current的帮助下访问这些变量。它允许您访问当前的Http Context,因为HttpContext.Current属性是静态的。

<强>即

//Assigning from one page
Session["Name"]="ABC";

//get the values from session variable in static method 
string name = HttpContext.Current.Session["Name"].ToString();

我只是为会话派生,其他人请参考@Fermin回答。

由于

答案 1 :(得分:0)

有许多方法可以在页面之间传递值。您已经提到过您不想使用Session,Cookies或QueryString但未提及原因,我将重点介绍几种不同的方法以及如何从静态方法中检索其值服务器端。

所有这些方法都使用具有静态.Current属性的HttpContext来访问当前正在执行的上下文。

// Cookie
var cookieVal = HttpContext.Current.Request.Cookies["TestCookie"].Value;

// Query String
var qString = HttpContext.Current.Request.QueryString["Test"];

// HTTP Header
var headerVal = HttpContext.Current.Request.Headers["x-my-header"];

// Form Data
var formVal = HttpContext.Current.Request.Form["form-property"];

// Session Data (you may need to decorate with [WebMethod(EnableSession=true)]
var session = HttpContext.Current.Session["sessionKey"];

// HTTP Context Item
// Note: This will only work if value set server side and page 2 called with Server.Transfer() rather than Response.Redirect()
var httpItem = HttpContext.Current.Items["itemKey"];
相关问题