How to get the Session value on the ashx page and the session on the ashx page

Source: Internet
Author: User

How to get the Session value on the ashx page and the session on the ashx page

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 does ashx store sessions? How does it get them in aspxcs?

On the front-end page, you can use Session ["TimeName"] to obtain your value. However, you cannot directly use session in ashx, you must first add an interface and a namespace reference. Using System. Web. SessionState; then implement your IRequiresSessionState interface. Otherwise, an exception will be thrown on the aspx page. Example: public class Test: IHttpHandler, IRequiresSessionState

How does ashx store sessions? How does it get them in aspxcs?

The same is true for normal sessions. There is nothing special about it. If you call it, you can directly use the CS file, which is of the same nature as aspx. cs.

Related Article

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.