The method of obtaining session in ASP.net ashx-practical skills

Source: Internet
Author: User

1-in ASPX and Aspx.cs, both are session["xxx"]= "AAA" and aaa=session["xxx". ToString () for reading and writing.

In Ashx, the session is to use Context.session, read and write method is this:

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

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

You may be prompted to add a reference to the namespace using System.Web.SessionState, otherwise the context. session ["XXX"] read out 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 web.ui,ashx can only be passed in the HttpContext instance to request and session and so on operation. Appcode in the CS file no Web.ui, no HttpContext parameters, if the CS can get the current HttpContext, can be based on this HttpContext to operate the request and session.

To operate the session in CS, the IRequiresSessionState interface should be added. The HttpContext class has a static property called Current, which you can use to get the present HttpContext.

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

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.