Asp. net handles the Session problem resolution in the ashx file

  • 2020-06-12 08:49:52
  • OfStack

Do a login instance of vb.net today, the foreground USES Ajax in Ext, the background USES ashx files to handle the login process, and storing the user in Session is always an error.

There are several reasons as follows:

1. ViewState object is Unll.

2. DateSet is empty.

3. DataReader is empty because of sql statement or Datebase.

4. String variables are declared without null values.

5. The object was not initialized with new.

6. Session object is empty.

Why you initially thought the Session object was empty: Change the code to the following

if context.Session.isNewSession then

context. Session("UserName")=username 'username is the accepted username

.................

After such modification, dizzy!! Same mistake, Google

ashx processing session in the search, it is not easy, it seems that the search also needs a lot of skills

Original: in ashx Session is used, we need to inherit System. Web. SessionState. IRequiresSessionState interface. The default of inherited IHttpHandler interface

OK look directly at the source code


<%@ WebHandler Language="VB" Class="logout" %>
Imports System
Imports System.Web
Imports System.Web.SessionState
Public Class logout : Implements IHttpHandler, IRequiresSessionState
   
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Session.Clear()
        context.Response.Write("{'success':'true'}") 'JSON Format data 
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class


Related articles: