When ihttphandler is used in ASP. NET to process requests (such as self-implemented Ajax), the reason why SESSION (or session is null) cannot be obtained and the Solution

Source: Internet
Author: User

Read this post first:

 

How can I capture a session in httpmodule?

Http://topic.csdn.net/u/20070226/10/9db317e8-0400-4d92-9c93-83a90d6bcde5.html

 

The following is an explanation on msdn:

Overview of ASP. NET application lifecycle in IIS 5.0 and 6.0

Http://msdn.microsoft.com/zh-cn/library/ms178473 (V = vs.80). aspx

 

When processing this requestHttpapplicationClass to execute the following events. Want to expandHttpapplicationClass developers need to pay special attention to these events.

  1. When a request is verified, the browser checks the information sent and determines whether it contains a potentially malicious flag. For more information, see validaterequest and script intrusion overview.

  2. If you have configured any URL in the urlmappingssection of the web. config file, perform URL ing.

  3. The beginrequest event is triggered.

  4. The authenticaterequest event is triggered.

  5. This triggers the postauthenticaterequest event.

  6. The authorizerequest event is triggered.

  7. This triggers the postauthorizerequest event.

  8. This triggers a resolverequestcache event.

  9. The postresolverequestcache event is triggered.

  10. Based on the file extension of the requested resource (ing in the application configuration file), select an ihttphandler class to process the request. If the request is for an object (PAGE) derived from the page class and needs to compile the page, ASP. NET will compile the page before creating an instance.

  11. The postmaprequesthandler event is triggered.

  12. The acquirerequeststate event is triggered.

  13. This triggers the postacquirerequeststate event.

  14. Raises the prerequesthandlerexecute event.

  15. Call the appropriateIhttphandlerClass processrequest method (or asynchronous beginprocessrequest ). For example, if the request is for a page, the current page instance processes the request.

  16. The postrequesthandlerexecute event is triggered.

  17. This triggers the releaserequeststate event.

  18. The postreleaserequeststate event is triggered.

  19. If the filter attribute is defined, the response is filtered.

  20. The updaterequestcache event is triggered.

  21. This triggers the postupdaterequestcache event.

  22. An endrequest event is triggered.


 

Select ihttphandler in step 1, generate httpsessionstate in step 2, and callIhttphandlerClass processrequest method. In other wordsIhttphandlerWhen the processrequest method of the class is called, httpsessionstate has been prepared for a long time. But why is it true that the result is null? The reason is that we didn't implement this for ourselves.IhttphandlerClass is displayed with "we want to use session "!

 

Solution:

Let the class that implements the ihttphandler interface you write implement another irequiressessionstate interface. This interface does not have anything, however, it will make your class implementing ihttphandler get a non-null httpsessionstate!

Let's take a look at the description of the irequiressessionstate interface:

Http://msdn.microsoft.com/zh-cn/library/system.web.sessionstate.irequiressessionstate.aspx

Specify the target HTTP handler to have read and write access to the session status value. This is a tag interface and there is no way.

Related Article

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.