【发布时间】:2014-01-08 06:37:18
【问题描述】:
现在我正在使用会话变量来存储一个布尔值,然后在我的视图中使用它。我知道 HttpContext.Session[] 变量是静态的,但这是否意味着我需要将它们存储在静态方法中,或者我可以将它们保存在我的操作过滤器中并直接从那里调用它们?
控制器:
public class AuthorizationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
// If not authorized, redirect to login page
if (true)
{
authorized = false;
filterContext.HttpContext.Session["authorized"] = authorized;
filterContext.Result = new RedirectResult("https://www.website.com");
}
else
{
filterContext.HttpContext.Session["authorized"] = authorized;
}
...
查看:
@{var authorized = (bool)Session["authorized"];}
@if (authorized != null)
{
if (authorized == true)
{
<li><a href="/">Download</a></li>
}
}
我是否应该将我的会话变量存储在下面的静态方法中并调用它们,而不是像上面那样做?
public static bool Authorized(){
return (bool)HttpContext.Current.Session["authorized"];
}
【问题讨论】:
标签: c# asp.net asp.net-mvc session