2013-04-25 2 views
0

MVC 프로젝트에 문제가 있습니다! 목표는 모든 컨트롤러에 전달하기 위해 세션 VAR을 설정하는 것입니다 : 을 내 xUserController 내부두 개의 다른 컨트롤러간에 세션 변수가 여전히 null로 유지됨

  Session["UserId"] = 52; 
      Session.Timeout = 30; 

      string SessionUserId = ((Session != null) && (Session["UserId"] != null)) ? Session["UserId"].ToString() : ""; 

// SessionUserId = "52"

을하지만 ChatMessageController 내

[HttpPost] 
public ActionResult AddMessageToConference(int? id,ChatMessageModels _model){ 

     var response = new NzilameetingResponse(); 
     string SessionUserId = ((Session != null) && (Session["UserId"] != null)) ? Session["UserId"].ToString() : ""; 
//... 

     } 
     return Json(response, "text/json", JsonRequestBehavior.AllowGet); 
} 

SessionUserId = ""

그래서,이 이유는 무엇입니까? 내 모든 컨트롤러 내에서 세션 변수를 전역으로 설정하는 방법 ??

+0

세션 varialbe는 글로벌 전용입니다. – Devesh

+0

물론, 다른 컨트롤러에서 SessionUserId = ""를 어떻게 설명합니까? 대신에 무엇을 써야합니까? – Bellash

+0

어떤 브라우저를 사용하고 있습니까? – Sharun

답변

0

내가 문제를 해결하는 방법입니다

내가 그것을 할 수있는 가장 좋은 방법이 아니다 알고 있지만 그것은 나를 도와 :

public class BaseController : Controller 
{ 
    private static HttpSessionStateBase _mysession; 
    internal protected static HttpSessionStateBase MySession { 
     get { return _mysession; } 
     set { _mysession = value; } 
    } 
} 
다음과 같이 내가 기본 컨트롤러를 만든

먼저

그런 다음베이스 컨트롤러 클래스에서 상속 받도록 다른 모든 컨트롤러의 코드를 변경했습니다.

은 그럼 아래와 같이 "OnActionExecuting"방법을 오버라이드 :

public class xUserController : BaseController 
{ 
    protected override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     BaseController.MySession = Session; 
     base.OnActionExecuting(filterContext); 
    } 
    [HttpPost] 
    public ActionResult LogIn(FormCollection form) 
    { 
     //---KillFormerSession(); 
     var response = new NzilameetingResponse(); 
     Session["UserId"] = /*entity.Id_User*/_model.Id_User; 
     return Json(response, "text/json", JsonRequestBehavior.AllowGet); 
    } 
} 

마지막으로, 내가 세션 변수를 호출하는 방식을 변경했습니다. 대신

string SessionUserId = ((Session != null) && (Session["UserId"] != null)) ? Session["UserId"].ToString() : ""; 

string SessionUserId = ((BaseController.MySession != null) && (BaseController.MySession["UserId"] != null)) ? BaseController.MySession["UserId"].ToString() : ""; 

지금은 작동하고 내 세션 바르 모든 컨트롤러를 가로 질러 걸어 갈 수 있습니다.

0

두 가지 이유가 있습니다 : 첫 번째 세션은 끝났고 두 번째는 세션 변수를 응용 프로그램의 다른 위치에서 다시 작성한다는 것입니다. 추가 코드가 없으면 더 말할 내용이 없습니다. 여기

+0

아니오를 참조하십시오! 제 대답을보세요, 어떻게 해결 했나요? – Bellash

관련 문제