Using session in Custom HttpHandler

Source: Internet
Author: User
Tags httpcontext implement interface reference
Session

A recent need to do a response to a specific request interface, only internal processing, there is no UI, the machine in this case, of course, using implementation IHttpHandler to process, you can reduce the time to load HTML controls. As it was, the two methods defined in IHttpHandler, ProcessRequest (HttpContext ctx) and Isrunable (), were seen in ProcessRequest (HttpContext CTX There is a httpcontext input parameter that could have been used for all server objects, only the previous reference to HttpContext. However, the problem arises, in this custom HTTP response processing header needs to write session, for other request and response can be referenced by the use of HttpContext, but the session is not, always appear the object is not referenced error, Really baffled, good HttpContext inside are listed in the server object can be used, but the session is not used! Really bitter, had to be custom HttpHandler content to do ordinary webform. After doing a good job, but inadvertently found in the custom HttpHandler to use the method of Session!

1, the first reference System.Web.SessionState this namespace,

2, if it is to read the content of the session in the HttpHandler, it is necessary to implement the IHttpHandler class in the same time ireadonlysessionstate this interface.

3, if it is to read and write the content of the session in the HttpHandler, it is necessary to implement the IHttpHandler class in the same time IRequiresSessionState

This allows you to use the session normally in a custom HttpHandler.



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.