0

Please help me to fix my problem. My problem is the HTTPContext.Current is always equal to null. Thanks in advance.. Please see below code

HttpContext _context = null;
public EventReceiver()
{
    _context = HttpContext.Current;
}
Sam Casil
  • 938
  • 1
  • 11
  • 16
  • http://stackoverflow.com/questions/1601352/how-to-obtain-the-httpcontext-in-event-handler wil give you answer why its Zero. – Shoban Apr 19 '12 at 06:26
  • @Shoban - `null`, not zero. It *may* happen that a `null` reference is implemented internally as a pointer with all bits `0`, and that interpreting that same pointer as an integer results in `0`, but those are implementation details. – Damien_The_Unbeliever Apr 19 '12 at 06:35

3 Answers3

2

Since you're receiving the event, the event has already occurred. By now, the context is gone.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Precisely what I said. By the time you're receiving the event, the event has already completed. The context in which the event occurred no longer exists. Think of an event being received as someone telling you what they heard on the phone after they hang up. The context is like the connection. – David Schwartz Apr 19 '12 at 06:33
  • 3
    I suggest that next time you ask a question on StackOverflow you explain your problem in detail and ask a specific question rather than just stating something you observed without explaining why that's an issue. – David Schwartz Apr 19 '12 at 07:05
  • Sorry guys..next tome i'll explain well my question. – Sam Casil Apr 20 '12 at 01:40
1

HttpContext.Current will not be available to your custom class EventReceiver unless you pass it from the class which has it like WebPage

Adil
  • 146,340
  • 25
  • 209
  • 204
  • If your pass HttpContext.Current to your class then before passing check if it is null ? – Adil Apr 19 '12 at 06:45
  • HttpContext.Current is for current http request do you get http request where you are accessing HttpContext.Current – Adil Apr 19 '12 at 06:55
0

You should not rely on context in event receivers. Instead you should get all objects from the properties of the event that are passed to the methods of the receivers as an argument.
see this question

Community
  • 1
  • 1
Maks Matsveyeu
  • 866
  • 5
  • 11