从静态方法访问 QueryString / Session?
我使用 ASP.Net 和静态 WebMethod / PageMethod 来执行一些异步工作。 我的问题是如何在这里访问我的 queryStrings 和 Session 变量?
我尝试了“HttpContext.Current”,这里提供了很多信息,但不是我的 QueryString 也不是我的 Session,我不知道为什么。
[WebMethod(EnableSession=true)]
public static object Update(string time)
{
string timer;
string lastBidder;
string price;
//Countdown timer
DateTime dt = DateTime.Parse(time);
dt = dt.AddSeconds(-1.0);
timer = dt.ToString("HH:mm:ss");
int auctionID = 6;
if (!int.TryParse(HttpContext.Current.Request.QueryString["id"], out auctionID))
throw new Exception("Seitenaufruf ohne ID");
Business.AuctionHandling ah = new Business.AuctionHandling();
DAL.Auktion auktion = ah.GetSingleAuction(auctionID);
price = auktion.AktuellerPreis.ToString("###0.00");
//this.gvHistory.DataBind();
List<DAL.Biethistorie> his = ah.GetBidHistoryForAuction(auctionID);
if (his.Count > 0)
{
lastBidder = his[0].Benutzer.Benutzername;
//History fett
//gvHistory.Rows[0].Font.Bold = true;
//gvHistory.Rows[0].ForeColor = System.Drawing.ColorTranslator.FromHtml("#3B4D5F");
//lblHöchstesGebot.ForeColor = System.Drawing.Color.Black;
}
else
{
lastBidder = Helper.StringHelper.AuctionDeatil_NoBidder;
//lblHöchstesGebot.ForeColor = System.Drawing.Color.Red;
}
return new
{
valueTimer = timer,
valuePrice = price,
valueLastBidder = lastBidder
};
}
I use ASP.Net and a static WebMethod / PageMethod to do some async work.
My question is how to access my queryStrings and Session variables here?
I tried "HttpContext.Current" and a lot of information is available here, but not my QueryString nor my Session and I don't know why.
[WebMethod(EnableSession=true)]
public static object Update(string time)
{
string timer;
string lastBidder;
string price;
//Countdown timer
DateTime dt = DateTime.Parse(time);
dt = dt.AddSeconds(-1.0);
timer = dt.ToString("HH:mm:ss");
int auctionID = 6;
if (!int.TryParse(HttpContext.Current.Request.QueryString["id"], out auctionID))
throw new Exception("Seitenaufruf ohne ID");
Business.AuctionHandling ah = new Business.AuctionHandling();
DAL.Auktion auktion = ah.GetSingleAuction(auctionID);
price = auktion.AktuellerPreis.ToString("###0.00");
//this.gvHistory.DataBind();
List<DAL.Biethistorie> his = ah.GetBidHistoryForAuction(auctionID);
if (his.Count > 0)
{
lastBidder = his[0].Benutzer.Benutzername;
//History fett
//gvHistory.Rows[0].Font.Bold = true;
//gvHistory.Rows[0].ForeColor = System.Drawing.ColorTranslator.FromHtml("#3B4D5F");
//lblHöchstesGebot.ForeColor = System.Drawing.Color.Black;
}
else
{
lastBidder = Helper.StringHelper.AuctionDeatil_NoBidder;
//lblHöchstesGebot.ForeColor = System.Drawing.Color.Red;
}
return new
{
valueTimer = timer,
valuePrice = price,
valueLastBidder = lastBidder
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
QueryString 位于 Request 属性中。
但会话就在那里:
The QueryString is in the Request property.
But the Session is in there:
出于兴趣,为什么不在调用 Web 方法时将所需的信息传递给它呢?
Out of interest why aren't you just passing the information you need to the web method as you are calling it?
我有类似的问题。我使用了许多静态方法来帮助管理我的缓存和会话。幸运的是,您可以将对 Cache 或 Session 的引用传递到您的方法中,如下所示:
然后使用 sessn 对象访问您的会话。
I had a similar problem. I had a number of static methods I was using to help manage my Cache and Session. Luckily, you can pass a reference to the Cache or Session into your moethods like this:
And then access your session by using the sessn object.
这是迟到的回复,但会帮助其他人并将其标记为答案..那么您必须发布有关如何调用该 Update 方法的代码。因为我也在做同样的事情,我得到了我的查询字符串,诀窍是你必须将它与你的 get 或 post 调用一起传递,就像下面的
$.ajax({
类型:“帖子”,
url: "" + getDirectoryPath() + getCurrentPageName() + "/SavePatientEpisodes?ApplicationInstanceID=" + 查询字符串,
数据: JSON.stringify({ PatientOne: PatientOneData, PatientTwo: PatientTwoData, PatientOneID: $("#tbPatient1").val(), PatientTwoID: $("#tbPatient2").val() }),
contentType: "application/json; charset=utf-8",
数据类型:“json”,
成功:函数(消息){
// 将 div 的内容替换为 page 方法的返回值。
}
});
并按如下方式访问它
_
公共共享函数 SavePatientEpisodes(ByVal PatientOne 作为列表(剧集)、ByVal PatientTwo 作为列表(剧集)、ByVal PatientOneID 作为字符串、ByVal PatientTwoID 作为字符串) As String
Dim dd As String = HttpContext.Current.Request.QueryString("ApplicationInstanceID")
return ""
结束功能
THIS IS LATE REPLY BUT WILL HELP OTHERS AND MARK IT AS ANSWER ..well u have to post your code on how you are calling that Update method. coz i am doing the same and im getting my querystring and the trick for that is you have to pass that in alongwith your get or post call like following
$.ajax({
type: "POST",
url: "" + getDirectoryPath() + getCurrentPageName() + "/SavePatientEpisodes?ApplicationInstanceID=" + querystring,
data: JSON.stringify({ PatientOne: patientOneData, PatientTwo: patientTwoData, PatientOneID: $("#tbPatient1").val(), PatientTwoID: $("#tbPatient2").val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
}
});
AND ACCESS IT as BELOW
_
Public Shared Function SavePatientEpisodes(ByVal PatientOne As List(Of Episode), ByVal PatientTwo As List(Of Episode), ByVal PatientOneID As String, ByVal PatientTwoID As String) As String
Dim dd As String = HttpContext.Current.Request.QueryString("ApplicationInstanceID")
return ""
End Function