2009-11-19 69 views
2

我使用ASP.Net和靜態WebMethod/PageMethod來完成一些異步工作。 我的問題是如何在這裏訪問我的queryStrings和Session變量?從靜態方法訪問QueryString/Session?

我試過「HttpContext.Current」和大量的信息可以在這裏找到,但不是我的查詢字符串,也沒有我的會議,我不知道爲什麼。

[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 
     }; 
    } 

回答

7

出於興趣,你爲什麼不只是路過,你需要在Web方法,您呼叫的信息呢?

+1

我同意,這也會讓單元測試靜態方法變得更容易,但是這不應該成爲一個評論嗎? – 2009-11-19 17:58:01

+0

我不在乎單元測試的時刻。所以你的意思是在JavaScript中訪問會話和查詢字符串,並將其傳遞給變量? – Kovu 2009-11-19 18:02:35

8

QueryString位於Request屬性中。

System.Web.HttpContext.Current.Request.QueryString 

但會議是在那裏:

System.Web.HttpContext.Current.Session 
+2

兩者都不在這裏工作! – Kovu 2009-11-19 18:00:41

0

我有一個類似的問題。我有很多靜態方法可以幫助我管理緩存和會話。幸運的是,你可以傳遞到高速緩存或會話的引用到您的moethods這樣的:

public static void DoSomething(System.Web.SessionState sessn) 

,然後使用該sessn對象訪問會話。

+0

如何從javascript中獲取sessionstate對象 – Kovu 2009-11-19 18:15:14

+0

對不起。我不太確定那個。會話通常由服務器處理。也許把它包含到一個隱藏的領域作爲JSON或什麼的?這可能是一個很好的話題,只是一個新的問題,而且有人擁有更多的JavaScript體驗,而不是我可以解決的問題。 – 2009-11-19 20:00:34

-1

這遲到的答覆,但會幫助他人,將其標記爲答案..well u必須張貼到你是如何調用該Update方法的代碼。怎麼把我做相同的,即時得到我的查詢字符串,併爲關鍵就在於,必須通過在非常久遠的GET或POST調用,比如以下

$阿賈克斯({ 類型:「POST」, 網址: 「」 + getDirectoryPath()+ getCurrentPageName()+ 「/ SavePatientEpisodes ApplicationInstanceID =?」 +查詢字符串, 數據:JSON.stringify({PatientOne:patientOneData,PatientTwo:patientTwoData,PatientOneID:$( 「#tbPatient1」)VAL() ,PatientTwoID:$(「#tbPatient2」)。val()}), contentType:「application/json; charset = utf-8」, dataType:「json」, success:function(msg){ //將div的內容替換爲頁面方法的返回值 } });

和訪問如下

_ 公共共享功能SavePatientEpisodes(BYVAL PatientOne方式列表(插曲),BYVAL PatientTwo方式列表(情節),BYVAL PatientOneID作爲字符串,BYVAL PatientTwoID作爲字符串)作爲字符串 昏暗DD的String = HttpContext.Current.Request.QueryString( 「ApplicationInstanceID」)

 Dim lang As Integer = toInt(HttpContext.Current.Session("UserID")) 

回報 「」 端功能