Access to QueryString / Session from a static Method?
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
};
}
The QueryString is in the Request property.
System.Web.HttpContext.Current.Request.QueryString
But the Session is in there:
System.Web.HttpContext.Current.Session
Out of interest why aren't you just passing the information you need to the web method as you are calling it?
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:
public static void DoSomething(System.Web.SessionState sessn)
And then access your session by using the sessn object.
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")
Dim lang As Integer = toInt(HttpContext.Current.Session("UserID"))
return "" End Function
精彩评论