How can I access session in a webmethod?
You can use:
HttpContext.Current.Session
But it will be null
unless you also specify EnableSession=true
:
[System.Web.Services.WebMethod(EnableSession = true)]
public static String checaItem(String id)
{
return "zeta";
}
Cannot get session in webmethod in asp.net
As I see, everything should be fine here.
As far as HttpContext.Current.Session is not null, session state is supported here.
Please ensure, that you set Session["PhotoId"].
You can test whether it is the same session by examining
HttpContext.Current.Session.SessionID
in both normal ASPX and WebMethod.
Set Session object inside a WebMethod (using EnableSession = true) not storing value
Found the solution. As the asp file doesn't contains a ScriptManager with property EnablePageMethods
set to true
, the session not maintains.
Why Can WebMethod Access Session State Without EnableSessionState?
On http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession(v=vs.90).aspx, you will see that this applies to XML Web services (i.e., classes derived from System.Web.Services.WebService).
[WebMethod(EnableSession=true)]
Because your page presumably extends System.Web.UI.Page, it is not necessary to explicitly enable the session. On http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.enablesessionstate.aspx, you can see that EnableSessionState is enabled by default for Pages (which you probably already know).
How to exchange session variables between webmethods in asp.net webservices
This is a double-post to how to exchange session or cookie variables between two webmethods in asp.net webservices , so here it comes again:
Disclaimer: I think that a web service that relies on Session state is just plain WRONG since a web service should be stateless. However:
At http://msdn.microsoft.com/en-us/library/aa480509.aspx you can read about how to use ASP.NET Session in a web service:
- Make sure that /configuration/system.web/sessionState in web.config is configured properly to enable session state
- Make sure that uses the web service has a cookie container where the ASP.NET session cookie can be stored. If the client is from a web browser (e.g. ajax call) this usually works out of the box, but if you are building a standalone client, you have to do some more work, see the link above.
All in all: a bad design decision gives you more work than necessary (sorry for rubbing it in).
I think you should redesign you web service so that you either always send username and password in all methods or add a login method that gives the client a token that is sent with each web service request.
How to use page session value and function in Jquery webmethod
You need to explicitly state that you want to use Session
with your ASP.NET AJAX Page Method by using the EnableSession= true
value in the WebMethod
attribute, like this:
[WebMethod(EnableSession = true)]
public static void Save()
{
UserInfo objUser = new UserInfo();
objUser.Useid = Convert.ToInt16(HttpContext.Current.Session["UId"]);
objUser.CalcUser = CalcUser();
... Save into Database
}
Note: You must fully qualify the namespace of the session (
HttpContext.Current.Session
).
To use the CalcUser()
function you need to make it static
and fully qualify the Session
object, like this:
public static int CalcUser()
{
return Convert.ToInt16(HttpContext.Current.Session["UId"]) * 2;
}
Note: ASP.NET AJAX Page Methods only have access to
static
methods, as there is no instance of the page (or any class for that matter).
How to modify the session variable inside the asp.net webmethod
In your code you should do like this, because count++ will assign the existing value and then increment its value
HttpContext.Current.Session["pgnum"] = ++count;
Related Topics
Get the Object Is Null Using JSON in Wcf Service
Unit Testing That Events Are Raised in C# (In Order)
Why Does System.Threading.Timer Stop on Its Own
JSON Deserialization - Map Array Indices to Properties with JSON.Net
How to Fill Part of Image with Color
Filter a List by Another List C#
ASP.NET Gridview: How to Edit and Delete Data Records
How to Redirect to Index from Another Controller
How to Render an ASP.NET MVC View in PDF Format
How to Parse Dates with a Suffix "Th", "St" or "Nd" on the Day of the Month
Finding Property Differences Between Two C# Objects
C#: Getting Names of Properties in a Chain from Lambda Expression
Sending Sms from an ASP.NET Website
Only Primitive Types or Enumeration Types Are Supported in This Context
Visual Studio 2015 Break on Unhandled Exceptions Not Working