我使用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
};
}
我同意,這也會讓單元測試靜態方法變得更容易,但是這不應該成爲一個評論嗎? – 2009-11-19 17:58:01
我不在乎單元測試的時刻。所以你的意思是在JavaScript中訪問會話和查詢字符串,並將其傳遞給變量? – Kovu 2009-11-19 18:02:35