2017-09-18 1 views
-1
public static CustomerInfo Customer 
{ 
    get 
    { 
     if (System.Web.HttpContext.Current.Session["CustomerData"] == null) 
     {      
      System.Web.HttpContext.Current.Response.Redirect("~/Account/Login"); 
      return new CustomerInfo(); 
     } 
     else 
     { 
      return (CustomerInfo)System.Web.HttpContext.Current.Session["CustomerData"]; 
     } 
    } 
    set 
    { 
     System.Web.HttpContext.Current.Session["CustomerData"] = value; 
    } 
} 

때마다 HttpContext.Current.Session["CustomerData"]하는 대신 예외를주고 계정 컨트롤러에서보기를 로그인하는 리디렉션의, null입니다.MVC5 라우팅 프로그래밍 C#을

+5

프로퍼티 게터와로 리디렉션

Return RedirectToAction("Login", "Account"); 

을 사용할 수 있습니다 부작용은 나쁜 생각입니다. – Amy

+0

세션을 확인하고 리디렉션하려면 [작업 필터] (https://www.codeproject.com/Articles/1095295/Check-Session-Timeout-by-Using-ActionFilters-in-MV)를 사용하십시오. –

+0

Steve에게 액션 필터를 사용하라는 제안을 해주셨지만, getter에서 로그인보기로 리디렉션 할 수 있습니다. – Madhurima

답변

-1

시도 : 컨트롤러 검사에서 다음

if (System.Web.HttpContext.Current.Session["CustomerData"] == null) 
    {      
     Session["CustomerLogin"] = "True"; 
     return new CustomerInfo(); 
    } 
    else 
    { 
     Session["CustomerLogin"] = "False"; 
     return (CustomerInfo)System.Web.HttpContext.Current.Session["CustomerData"]; 
    } 

:

if(Convert.ToString(Session["CustomerLogin"]) == "True"){ 
    return RedirectToAction("Login", "Account"); 
} 
+0

그의 구현에서 그렇게 할 수 없다 - 그는 정적 클래스의 getter에서 이것을하려고한다. 이것은'CustomerInfo'를 반환한다. – Alex

+0

오, 네 말이 맞다. 그런 다음 그는 CustomerInfo가 리턴하고 그에 따라 리디렉션되는 것을 확인해야합니다. – Laiman

+0

안녕 Laiman는 답장을 보내 주셔서 감사합니다. 이 public static 메서드는 public class CustomerInformation 안에 있으며 RedirectToAction이 현재 컨텍스트에 존재하지 않는다는 예외가 있습니다 – Madhurima

0

당신은 다른 컨트롤러 및 방법

+0

여기서는 할 수 없습니다. 그게 내가 처음 생각뿐만 아니라 그는 그의 정적 클래스 getter에서 이것을하려고합니다. CustomerInfo()를 반환합니다. – Laiman

관련 문제