2011-12-22 4 views
1

내가 사용하고 있습니다 : 개체의 인스턴스로 설정되지 않았습니다 개체 참조 :HttpContext에서 세션을 올바르게 설정하는 방법은 무엇입니까?

if (string.IsNullOrEmpty(myHttpContext.Request.QueryString["code"])) 
    { 

     if (myHttpContext.Session == null || myHttpContext.Session["code"] == null) 
     { 
      OutputError("Code", "Invalid code."); 
     } 
     else 
     { 
      code = myHttpContext.Session["code"].ToString(); 
     } 
    } 
    else 
    { 
     code = myHttpContext.Request.QueryString["code"]; 
     myHttpContext.Session.Add("code", code); 
    } 

그러나 나는 오류가 계속.

대상 : myHttpContext.Session.Add ("code", code);

내가 원하는 것은 간단한 세션을 설정하는 것입니다. 누군가 제발 나를 미치게 만듭니다.

답변

4

IHttpHandler (ashx) 클래스를 구현 했습니까 IRequireSessionState? 그렇지 않으면 Session 객체에 액세스 할 수 없습니다.

public class MyHandler : IHttpHandler, IRequireSessionState 
{ 
    public bool IsReusable { get { return false; } } 

    public void ProcessRequest(HttpContext ctx) 
    { 
     // your code here 
    } 
} 
+0

몇 년 후, 이것은 첫 번째 게시물은이 솔루션을 준 것으로 나타났습니다. 그것은 더 많은 upvotes가 필요합니다. :) 제 경우에는 제가 사용한 인터페이스는'System.Web.SessionState.IReadOnlySessionState'였습니다. – Andrew

0

당신은 대신 항상 존재하는 상황 사용할 수도 있습니다 그러나

if (myHttpContext != null)을 잊었 : 세션에 대한 HttpContext.Current

// if querystring parameter has a value 
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["code"])) 
    // then use its value 

같은를 (그것이 현재의 경우 - @davisleeps가에 대한 답변을 참조) https://stackoverflow.com/a/8598088/179972

+0

시도 할 수도 있습니다 생각합니다. – Darren

+0

잘 작동합니다. 나는 이걸로 간단한 세션을 할 수 없다는 것을 믿을 수 없어, 나는 쿠키를 사용해야 할거야. – Darren

+0

원래 질문에는 없지만 확실하지 않은 경우 null도 확인하십시오. 문제와 관련이 있다고 생각되면 질문을 업데이트하여 자세한 정보와 코드를 제공 할 수 있습니다. –

0

난 당신이 내가 가진 모든 CurrentHandler 및 CurrentNotification입니다

myHttpContext.Session["code"] = code; 
관련 문제