How to get the session value on the ashx page

Source: Internet
Author: User

On the general transaction processing page, you can easily obtain the request and response objects and perform the following operations:

Httprequest request = context. request;

Httpresponse response = context. response;

However, it is not that easy to obtain the session value. For example, you need to obtain the Logon account session ["useraccount"] stored in the session in ashx.

If you only use context. session ["useraccount"], an exception will be reported "the object has not been referenced to the instance of the object ".

Therefore, if you want to obtain the value in the session, you need

1. Introduce the namespace:

Using system. Web. sessionstate;

2. Implement the irequiressessionstate interface as follows:

/// <Summary>
/// $ Codebehindclassname $ abstract description
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class adduserinfo: ihttphandler, irequiressessionstate // This is the implementation of the display. No implementation method is required.
{

Public void processrequest (httpcontext context)
{

//...

// You can perform the following operations:

If (context. session ["useraccount"]! = NULL)

{

String account = context. session ["useraccount"]. tostring ();

}

//... Continue the following code

}

}

How to get the session value on the ashx page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.