ASP.NET ASHX method to obtain Session

  • 2020-07-21 07:22:42
  • OfStack

1- In aspx and aspx.cs, both are written as Session["xxx"]="aaa" and aaa=Session["xxx"].ToString ().

In ashx, Session USES context. Session, which reads and writes like this:

context.Session["xxx"]="aaa" and aaa=context.Session["xxx"].ES27en ()

2- To read and write Session successfully in ashx files, the interface IRequiresSessionState should be added after using class's

May be prompted to add a reference to the namespace using System.Web.SessionState), otherwise context.Session ["xxx"] always reads null

aspx. cs is Request and Session directly, and in ashx is context Request and context Session. aspx. Can be used directly in the cs, because a Web. UI, instances can only rely on the transmission in HttpContext ashx Request and Session for operation. cs file in AppCode has no ES66en.UI and no HttpContext parameter. If the current HttpContext can be obtained in cs, you can operate Request and Session according to this HttpContext.

To operate Session in cs, the IRequiresSessionState interface should be added. There is a static attribute in the HttpContext class called Current that can be used to get the current HttpContext.

In AppCode cs file to get the current visitor IP, can use HttpContext. Current. Request. UserHostAddress.


Related articles: