2

我使用 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
        };
    }
4

4 回答 4

8

QueryString 位于 Request 属性中。

System.Web.HttpContext.Current.Request.QueryString

但是会话在那里:

System.Web.HttpContext.Current.Session
于 2009-11-19T17:47:33.457 回答
7

出于兴趣,您为什么不只是在调用 Web 方法时将所需的信息传递给它?

于 2009-11-19T17:49:54.417 回答
0

我有一个类似的问题。我使用了许多静态方法来帮助管理我的缓存和会话。幸运的是,您可以将 Cache 或 Session 的引用传递给您的方法,如下所示:

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

然后使用 sessn 对象访问您的会话。

于 2009-11-19T18:08:31.273 回答
-1

这是迟到的回复,但会帮助其他人并将其标记为答案..您必须发布您的代码,说明您如何调用该更新方法。因为我也在做同样的事情,我得到了我的查询字符串,诀窍是你必须将它与你的 get 或 post 调用一起传递,如下所示

$.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", 成功: function (msg) { // 将 div 的内容替换为 page 方法的返回值。} });

并按以下方式访问它

_ 公共共享函数 SavePatientEpisodes(ByVal PatientOne 作为列表(剧集),ByVal PatientTwo 作为列表(剧集),ByVal PatientOneID 作为字符串,ByVal PatientTwoID 作为字符串)作为字符串 Dim dd 作为字符串 = HttpContext.Current.Request.QueryString(“应用程序实例 ID")

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

return "" 结束函数

于 2012-12-18T01:33:39.380 回答