How to get session in ASP. NET Ashx

Source: Internet
Author: User

1-in ASPX and Aspx.cs, both are session["xxx"]= "AAA" and aaa=session["xxx"]. ToString () to read and write.

In Ashx, the Session uses context.session, and the reading and writing methods are:

Context. session["xxx"]= "AAA" and Aaa=context. session["XXX"]. ToString ()

2-In the Ashx file, to successfully read and write to the session, you should add the interface IRequiresSessionState after using the session class (adding

You may be prompted to add a namespace using the System.Web.SessionState reference) otherwise context. Session ["xxx"] read out is always null

Aspx.cs is the direct request and session, while in the ASHX is the context. Request and context. Session.aspx.cs can be used directly, because there are only web.ui,ashx in the HttpContext instance of the request and session to operate. Appcode in the CS file does not have Web.ui, there is no HttpContext parameter, if CS can get the current HttpContext, you can according to this HttpContext to operate the request and session.

To operate the session in CS, the IRequiresSessionState interface should be added. There is a static property called current in the HttpContext class, which can be used to get the present HttpContext.

In the Appcode CS file to get the current visitor's IP, you can use HttpContext.Current.Request.UserHostAddress.

How to get session in ASP. NET Ashx

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.