Search code examples
c#asp.netajaxwebformswebmethod

Are there alternatives to WebMethod in order to bypass postbacks but access Session


So i'm trying to improve performance in our ASP.Net Webforms application.

As usual Postbacks do their best to slow down performance. I've come to learn about WebMethod calls which i managed to get working too. However for my particular scenario i need to access the Session, which is obviously not possible in a static method.

So now my idea was writing a HttpModule to solve this. However this seems like quite an effort, so i was wondering if any of you know about an out of the box solution for this task?

Mind you, this is just a question about wether something already exists. Writing the module would be pretty straight forward. I'd just like to avoid reinventing the wheel.


Solution

  • if your problem is access to Session object from static member you can use HttpContext.Current.Session["..."]

    [WebMethod(EnableSession = true)]
    public static string Test()
    {
        string s = HttpContext.Current.Session["Test"].ToString();
        return s;
    }
    

    refer here for documentation about HttpContext.Current Property; inside that property you have also Request,Response and so on.

    As suggested by @Martin Smellworse

    Wherever you set the session, you need EnableSessionState="ReadOnly" and Async="true" in the page declaration