Asp.net solves Session problems in the ashx File

Source: Internet
Author: User

Today, we create a vb.net login instance. The foreground uses Ajax in Ext. The background uses the ashx file to process the login process and stores the user in the Session. This error always occurs. [NullReferenceException: object reference is not set to the instance of the object.]

I checked it online for the following reasons:

1. The ViewState object is Unll.

2. DateSet is empty.

3. DataReader is empty due to SQL statements or Datebase.

4. When a string variable is declared without a null value, the variable is applied.

5. The new initialization object is not used.

6. The Session object is empty.

At first, the reason was that the Session object was empty: Modify the following code:

If context. Session. isNewSession then

Context. Session ("UserName") = username 'username is the user name accepted

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

After the modification, the system will be dizzy !!!!!, The same error. Google again.

Input: It is too difficult to search the ashx processing session. It seems that searching also requires a lot of skills.

Originally: to use Session in ashx, The System. Web. SessionState. IRequiresSessionState interface must be inherited. The IHttpHandler interface is inherited by default.

OK. Check the source code directly.
Copy codeThe Code is as follows:
<% @ 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

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.