从静态方法访问 QueryString / Session?

发布于 2024-08-12 05:22:23 字数 1671 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2024-08-19 05:22:23

QueryString 位于 Request 属性中。

System.Web.HttpContext.Current.Request.QueryString

但会话就在那里:

System.Web.HttpContext.Current.Session

The QueryString is in the Request property.

System.Web.HttpContext.Current.Request.QueryString

But the Session is in there:

System.Web.HttpContext.Current.Session
对不⑦ 2024-08-19 05:22:23

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

Out of interest why aren't you just passing the information you need to the web method as you are calling it?

埋情葬爱 2024-08-19 05:22:23

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

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

然后使用 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:

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

And then access your session by using the sessn object.

溺ぐ爱和你が 2024-08-19 05:22:23

这是迟到的回复,但会帮助其他人并将其标记为答案..那么您必须发布有关如何调用该 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")

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

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")

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

return ""
End Function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文