asp.net the solution to the session problem in the Ashx file _ practical skills

Source: Internet
Author: User

Today do a vb.net login instance, the foreground uses the EXT Ajax, the backstage uses the Ashx file to process the login process, and the user stores in the session always appears in this error. [NullReferenceException: Object references are not set to an instance of an object. ]

Check it from the Internet for several reasons:

1, ViewState object is UNLL.

2, Dateset empty.

3, the SQL statement or datebase causes DataReader empty.

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

5, the object is not initialized with new.

6, the Session object is empty.

Reason to start thinking that the session object is empty: Modify the code below

If context. Session.isnewsession Then

Context. Session ("UserName") =username ' UserName for accepted username

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

After this modification, Halo!!!!!, is still the same mistake. Google it.

Input: ASHX processing session in the search, too difficult, it seems that search also requires a lot of skills

Original: In ashx to use session, you need to inherit the System.Web.SessionState.IRequiresSessionState interface. The default inherits the IHttpHandler interface

OK, just look at the source code.

Copy Code code 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.